Hi again Monks.

Well, depending on your definition, what I am writing may not be a daemon. However, what I want is for my script to be running in the background and for it to be unique in the process list.

I came across some behaviour that I do not understand, and am seeking clarification.

  • Why is the PID not being written to the file until after the script is HUPped?
  • How can I get it to write the PID into the file, so that I can check it externally?
    The point being that a third party script (or process) can run which checks that the PID in the file matches that in the process list. Pretend, if you will, that I am overly paranoid and must be assured that there is only ever one of these scripts running. :)
    #!perl -w use strict; use Fcntl qw(LOCK_EX LOCK_NB); my $PID_FILE = '/tmp/_pid_file'; my $TASK = 'test_script'; my $EVENT_DELAY = 30 ; my $DEBUG = 'YES'; eval { main( @ARGV ) }; error( $@ ) if $@; # -------------------------------------------------------------------- +---------- sub main { my $start_mess = "===== Starting $TASK"; $start_mess .= " (@_)" if @_; $start_mess .= " ====="; error( $start_mess ); open( HIGHLANDER, ">>$PID_FILE") or die( "Cannot write to $PID_FIL +E: $!" ); { 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." ); } } print HIGHLANDER $$; my $stopped; local $SIG{HUP} = sub { $stopped = 1 }; while( not $stopped ) { error( "DEBUG: Script Running" ) if $DEBUG; sleep $EVENT_DELAY; } my $finish_mess = "===== Stopping $TASK ====="; error( $finish_mess ); close HIGHLANDER; } # -------------------------------------------------------------------- +---------- sub error{ print "$TASK -> WARN: @_\n" }
    Please bear in mind that the code has been altered somewhat to work for this particular snippet.

    <a href="http://www.graq.co.uk">Graq</a>


    In reply to Writing to flock files in a daemon by graq

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.