[Chicago-talk] pid files and whatchamacalits.
Steven Lembark
lembark at wrkhors.com
Tue Dec 9 22:32:15 CST 2003
-- "Dooley, Michael" <Dooley.Michael at con-way.com>
> I have a perl script that runs via cron.
>
> outside of checking for a pid file does anyone have any cleaner ways of
> checking to see if the script is already running?
>
> I was looking at Proc::PID_File. but I would like to try and stay with the
> default install of perl.
> This is perl, v5.8.0 built for sun4-solaris
>
> Is below a standard way to do this?
>
> if (-e "/tmp/pid.file") {
> exit;
> } else {
> open (PID, ">/tmp/pid.file");
> print PID $$;
> close PID;
> }
Logic race: you can have multiple proc's checking the file's
existance and blowing up trying to create the file...
Check File::Temp for a better way (hands back a path and
open FD in one pass).
use File::Temp;
use File::Basename;
my $base = basename $0;
if( my($fh,$path) = tempfile $base . "XXX", DIR => '/var/run' )
{
print $fh $$
close $fh;
print STDERR "Pidfile: $fh";
}
...
unlink $path;
This'll give you a guaranteed pidfile.
If what you really want is a semaphore file then consider
using either a directory (mkdir is a system call on *NIX
and is therefore atomic) or a samaphore to test for the
running program.
--
Steven Lembark 2930 W. Palmer
Workhorse Computing Chicago, IL 60647
+1 888 359 3508
More information about the Chicago-talk
mailing list