Thanks for the hint so far. I have modified the code, as you suggested. Changed the
open to truncate instead of append, and tried using
select.
And yes, /tmp/_pid_file contains the PID.
This is fine until I run the script a second time, when /tmp/_pid_file gets truncated even though startup fails.
Any further suggestions?
# As before ..
# ...
sub main
{
my $start_mess = "===== Starting $TASK";
$start_mess .= " (@_)" if @_;
$start_mess .= " =====";
error( $start_mess );
my $FH;
open( HIGHLANDER, ">$PID_FILE") or die( "Cannot write to $PID_FILE
+: $!" );
{
my $count = 0;
{
flock HIGHLANDER, LOCK_EX | LOCK_NB and last;
sleep 1;
redo if ++$count < 3;
error( "Script is already running." );
die( "Startup failed, PID file locked." );
}
}
$FH = select(HIGHLANDER);
$| = 1;
select($FH);
print HIGHLANDER $$;
# ..
# ..as before..
<a href="http://www.graq.co.uk">Graq</a>