I make my scripts write out their PID to a file when they start after they have checked they are unique. The check consists of seeing if the pid file exists, reading it and checking if the process on that PID looks like an instance of my script (just in case the OS re-used the pid for something else) clean exit will clear up the pid file. Here is the check...
# See if I am already running # $my_name was derived earlier from $0 # $pidfile is set up in the scripts headers if (-f $pidfile) { open PID, $pidfile; my $pid = <PID>; close PID; print "read pid $pid\n" if $debug; open PROCS, "ps -ef |"; <PROCS>; # loose the header while (<PROCS>) { (undef, my $this_pid) = split; if ($this_pid == $pid) { print "pid in use $_\n"; # check this realy is us, not pid re-use by OS last unless /$my_name/; die "looks like an instance is already running, exiting\n" +; } } } open PID, ">$pidfile"; print PID $$; close PID;
That is the framework I use, it is a cut and paste from a larger script so will not run in isolation, sorry it is Friday and I want to go home so I can pack for my sailing holiday that starts tomorrow SEG
Cheers,
R.
In reply to Re: Checking for multiple instances
by Random_Walk
in thread Checking for multiple instances
by MarkNo1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |