I have a program that uses perl’s File::PID but I was having some issue where the program would occasionally start multiple times.
Finally today I spent some time to figure out what’s going on. It seems that if you use the '$pidfile->write'
function after checking whether it’s running already with '$pidfile->running()'
then it puts back NOT the PID of the currently running process, but the PID that was there before!
Unless I’m misunderstanding my test result, you (I) need to do this to get the new PID into the old pidfile:
use File::Pid;
my $pidfile = File::Pid->new({
file => '/var/run/upload_cdn.pid',
});
my $num = $pidfile->running();
if ( $num ){
die "already running: $num\n\n";
} else {
$pidfile->remove;
my $pidfile2 = File::Pid->new({
file => '/var/run/upload_cdn.pid',
});
my $pid = $pidfile2->write;
if( $pid ){
&_log("Now running with PID $pid\n");
}
}
#$pidfile->write; ## This was putting the original non-running PID back into the file
Maybe I was doing something wrong or maybe there’s a better way to handle this? Hopefully my program won’t start running multiple times any more!
2 Responses to perl’s File::PID trickiness