in reply to self detecting script

This should work
die "a horrible death" if ( -f "lockfile"); open LOCK, ">", "lockfile" or die; close (LOCK); ... do some stuff ... unlink("lockfile");
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re^2: self detecting script
by Fletch (Bishop) on Sep 15, 2005 at 18:26 UTC

    Nice race condition you've managed to write there. If you're going to try and roll your own make sure you do it correctly with sysopen and O_WRONLY|O_EXCL. See the docs, specifically perldoc -f sysopen and perldoc perlopentut.

Re^2: self detecting script
by runrig (Abbot) on Sep 15, 2005 at 18:56 UTC

    Probably not ideal, but this is almost a not too bad method when you know that separate instances of the same program will always start not less than a minute apart. There should be no race conditions. But if for some reason the previous instance never unlinks the file, no subsequent instance will run, which may or may not be what you want. Or if you manually start up the process at the same time cron kicks it off, you may run into a race condition. It may be best to open the file unconditionally and then use lock to get an exclusive lock on the file. Or use one of the previously mentioned modules to do that for you.

    I'll also mention that we do that sort of thing alot in our shell scripts, but it's the only solution since we can't call lock from the shell (also perl is not installed on many of our customer's systems). Unfortunately, we also use this method in shell scripts outside of cron where race conditions are possible, and some sort of file lock would be better.