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.
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.
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;
}