Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Determine if script is already running

by Abigail-II (Bishop)
on Mar 03, 2003 at 00:59 UTC ( [id://239923]=note: print w/replies, xml ) Need Help??


in reply to Determine if script is already running

Some obvious problems: there's a race condition. If two instances start at the same time, they might both determine the lockfile isn't there (the -e failing), and both create the file and write the PID in it.

Furthermore, if the program doesn't get the chance to run the END block (kill -9, exec), it might happen that the next time an instance is run, it finds the lockfile, and it just happens there's an unrelated process running with the same PID as mentioned in the PID file.

I'd say, open the file for read/write/create, and then try to gain an exclusive lock (non-blocking). If you get the lock, you're the only instance running. Keep the lock until the program terminates. Regardless how the process terminates, the kernel will release the lock (but make sure you pick a local file, not one mounted by NFS).

Abigail

Replies are listed 'Best First'.
Re: Re: Determine if script is already running
by Nitrox (Chaplain) on Mar 03, 2003 at 01:52 UTC
    Abigail, I tried to code for the possibility of a kill -9, (and Ctrl-C for that matter) and looked right past the simplicity of using a persistent lock for the scripts run duration. Thanks for clearing the fog. :)

    -Nitrox

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://239923]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-11-06 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (32 votes). Check out past polls.