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>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.