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.
Please bear in mind that the code has been altered somewhat to work for this particular snippet.#!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" }
In reply to Writing to flock files in a daemon by graq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |