graq has asked for the wisdom of the Perl Monks concerning the following question:
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" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Writing to flock files in a daemon
by zengargoyle (Deacon) on Mar 07, 2002 at 09:34 UTC | |
|
Re: Writing to flock files in a daemon
by dws (Chancellor) on Mar 07, 2002 at 09:38 UTC | |
by graq (Curate) on Mar 07, 2002 at 16:11 UTC | |
by bluto (Curate) on Mar 07, 2002 at 17:36 UTC | |
|
Re: Writing to flock files in a daemon
by fokat (Deacon) on Mar 08, 2002 at 00:08 UTC |