Lock file question

Lock file question

am 23.06.2011 00:02:31 von Steven Buehler

------=_NextPart_000_0073_01CC30FE.2CBE7ED0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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";

}



Thanks

Steve


------=_NextPart_000_0073_01CC30FE.2CBE7ED0--

Re: Lock file question

am 23.06.2011 00:34:42 von Jim Gibson

On 6/22/11 Wed Jun 22, 2011 3:02 PM, "Steven Buehler"
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).



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

RE: Lock file question

am 23.06.2011 01:08:16 von Steven Buehler

> -----Original Message-----
> From: Jim Gibson [mailto:jimsgibson@gmail.com]
> Sent: Wednesday, June 22, 2011 5:35 PM
> To: beginners@perl.org
> Subject: Re: Lock file question
>
> On 6/22/11 Wed Jun 22, 2011 3:02 PM, "Steven Buehler"
>
> 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]}



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

Re: Lock file question

am 23.06.2011 01:40:50 von jwkrahn

Steven Buehler wrote:
>
>> From: Jim Gibson [mailto:jimsgibson@gmail.com]
>>
>> On 6/22/11 Wed Jun 22, 2011 3:02 PM, "Steven Buehler"
>>
>> 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/