Forum Moderators: coopster & phranque

Message Too Old, No Replies

Working with process information

Allowing only one instance of a process to run

         

runner

4:55 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



I'm trying to set up some perl scripts so that only one instance of the script can be running at any time.

Right now I'm creating a lock file and placing the PID in the file. On script startup, I check to lock file and if it already exists, I check to see if the PID is still valid. If it's a valid process, I sleep until the existing process is gone.

This seems to work OK but I'd rather eliminate the need for any file processing. I'd rather just check to see if there is an existing process running that has the same name as my script.

Problem is that I can't figure out how to check for a process name. This does not seem to be a common thing.

runner

5:13 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



To clarify what I'm trying to do, I've included a shell script command of what I'd like to implement in perl. The following script returns the number if instances of a perl script named test.pl that are running.

ps -ef ¦ grep perl ¦ grep "test.pl" ¦ grep -c -v grep

I guess I could make a system call from within the perl script to run the command above... then I wouldn't have to dork around with managing lock files.

Just wanted to know if there was a way to accomplish this in perl.

wruppert

11:29 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



I would try something like [search.cpan.org...] .

moltar

4:44 am on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IMO checking for PID is better. Even Apache does this.

LeDroid

3:54 am on Mar 2, 2005 (gmt 0)



I'm using your first idea, it works fine but it's linux/unix only:

my $prog = ("$0" =~ /(.*\\¦.*\/)*(.*)$/) && "$+";
$run = `ps -ef¦grep perl¦grep $prog¦grep -vc grep`;
if ($run > 1) {
print dt_hre() . "ERROR: $prog already running.\n";
exit -1;
}