Steven Buehler wrote:

From: Jim Gibson [mailto:jimsgib...@gmail.com]

On 6/22/11 Wed  Jun 22, 2011  3:02 PM, "Steven Buehler"
<st...@ibushost.com>
scribbled:

I am trying to use a lockfile so that the script can't be run again if
it is already running.  The below code works fine.  The problem is
that if my script runs with an argument for a config file with one
person trying to run it as "script.pl first.cfg" and the second person
trys to run it as "script.pl second.cfg" the second person can't run
it.  How do I do a lock in this case so that as long as the config
files are different, it will still run?  The below code is great since
if the script is terminated, the lock goes away.  If I actually create
a file and put a lock on it and terminate the script before deleting
the lock file, I have to delete the lock file manually, correct?



use Fcntl ':flock'; #import LOCK_* constants

INIT{

open *{0} or die "What!? $0;$!";

flock *{0}, LOCK_EX|LOCK_NB or die "$0 is already running
somewhere!n";

}

I think you want to lock the config file ($ARGV[0]), not the program file
($0).


Thank You, but......
if I replace *{0} with any of the following, it goes to the "What" error or
a general error for a misconfiguration.  Yes, I double checked and made sure
the config file was there and it did read it if I took out the lock stuff.
$ARGV[0]
($ARGV[0])
*{$ARGV[0]}


You are using the single argument form of open() but you should just use the standard three argument form:

use Fcntl ':flock'; #import LOCK_* constants

INIT{
open LOCK_FH, '<', $0 or die "What!? $0;$!";
flock LOCK_FH, LOCK_EX|LOCK_NB or die "$0 is already running somewhere!n";
}


Or with @ARGV:

use Fcntl ':flock'; #import LOCK_* constants

INIT{
open LOCK_FH, '<', $ARGV[0] or die "What!? $ARGV[0];$!";
flock LOCK_FH, LOCK_EX|LOCK_NB or die "$ARGV[0] is already running somewhere!n";
}




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to