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