Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Using Proc::PID_File and Proc::Daemon should get you most of the way there. Here is a chopped down version of a daemon script that I use, complete with logging. Hope it is a helpful starting point.

#!/usr/bin/perl use strict; use warnings; use constant LOG_DIR => '/var/log/mydaemon'; use constant LOG_FILE => 'mydaemon.log'; use constant PIDDIR => LOG_DIR; use Proc::PID_File; use Proc::Daemon; use Log::Dispatch; use Log::Dispatch::File; use Date::Format; use File::Spec; sub dienice ($); # # fork and background process # our $ME = $0; $ME =~ s|.*/||; our $PIDFILE = PIDDIR."/$"; startDaemon(); # # Setup a logging agent # our $HOSTNAME = `hostname`; chomp $HOSTNAME; my $log = new Log::Dispatch( callbacks => sub { my %h=@_; return Date::Format::time2str('%B % +e %T', time)." ".$HOSTNAME." $0\[$$]: ".$h{message}."\n"; } ); $log->add( Log::Dispatch::File->new( name => 'file1', min_level => 'warning', mode => 'append', filename => File::Spec->catfile( +LOG_DIR, LOG_FILE), ) ); $log->warning("Starting Processing: ".time()); # # Setup signal handlers so that we have time to cleanup before shuttin +g down # my $keep_going = 1; $SIG{HUP} = sub { $log->warning("Caught SIGHUP: exiting gracefully") +; $keep_going = 0; }; $SIG{INT} = sub { $log->warning("Caught SIGINT: exiting gracefully") +; $keep_going = 0; }; $SIG{QUIT} = sub { $log->warning("Caught SIGQUIT: exiting gracefully" +); $keep_going = 0; }; #$SIG{TERM} = sub { $log->warning("Caught SIGTERM: exiting gracefully +"); $keep_going = 0; }; # # enter main loop # while ($keep_going) { # do something useful here } # # Mark a clean exit in the log # $log->warning("Stopping Processing: ".time()); # # startDaemon # # Fork and detach from the parent process # sub startDaemon { # # Fork and detach from the parent process # # eval { close DATA; }; # having __END__ will trigger __DATA__ to ope +n and should be closed eval { Proc::Daemon::Init; }; if ($@) { dienice("Unable to start daemon: $@"); } # # Get a PID file # dienice("Already running!") if hold_pid_file($PIDFILE); } # # dienice # # write die messages to the log before die'ing # sub dienice ($) { my ($package, $filename, $line) = caller; $log->critical("$_[0] at line $line in $filename"); die $_[0]; }

In reply to Re: Writing a Perl Daemon by cees
in thread Writing a Perl Daemon by tomazos

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and a soft breeze sighs...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2018-05-24 16:50 GMT
    Find Nodes?
      Voting Booth?