Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Writing a Perl Daemon

by cees (Curate)
on Jul 28, 2005 at 13:42 UTC ( [id://478964]=note: print w/replies, xml ) Need Help??


in reply to Writing a Perl Daemon

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."/$ME.pid"; 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]; }

Replies are listed 'Best First'.
Re^2: Writing a Perl Daemon
by onkar (Novice) on Dec 15, 2009 at 06:37 UTC
    # # startDaemon # # Fork and detach from the parent process # sub startDaemon { # # Fork and detach from the parent process # 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]; }
    I am not getting any error ! but the daemon is also not running ? What might be wrong ! I tried to debug it , but it hangs !
Re^2: Writing a Perl Daemon
by northshorefiend (Initiate) on Mar 15, 2013 at 11:35 UTC
    One gotcha that got me is if you are using sockets to talk to the daemon, then remember to catch SIGPIPE, or ignore it:
    $SIG{PIPE} = 'IGNORE';
Re^2: Writing a Perl Daemon
by Anonymous Monk on Apr 17, 2013 at 15:59 UTC
    You forgot to include your hold_pid_file function - prob why it doesn't work.
Re^2: Writing a Perl Daemon
by onkar (Novice) on Dec 15, 2009 at 05:52 UTC
    DB<1> stop in Proc::Daemon::Init Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<2> Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. ######### Forked, but do not know how to create a new TTY. ######### Since two debuggers fight for the same TTY, input is severely entangled. I know how to switch the output to a different window in xterms and OS/2 consoles only. For a manual switch, put the name of the created TTY in $DB::fork_TTY, or define a function DB::get_fork_TTY() returning this. On UNIX-like systems one can get the name of a TTY for the given window by typing tty, and disconnect the shell from TTY by sleep 1000000. {pid=5701} DB<2> s

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://478964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found