use File::Pid; # Check to make sure we are not already running my $pidfile = File::Pid->new({file => "/path/to/my.pid"}); # This next line gets the current pid in $pidfile # If nothing was running, we should get back our own pid my $pid = $pidfile->write; # now, die if the pid file cannot be opened, # or the pid running is not THIS INSTANCE # note: this can be overridden if $FORCE is true. die ("running process=".$pid.", my pid=$$\n") if (!$pid or ($pid != $$ and !$FORCE)); # ... do a bunch of stuff for a long time $pidfile->remove or warn "Couldn't unlink pid file\n";