smist has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm trying to use the generic daemon from:
Generic Daemon CPAN

I'm starting with trying the example from the module. I cannot get the daemon to run. I get no error messages; just a message saying the daemon is starting. When I run the "check" subroutine it says that the config is fine and the daemon is not running. Here is the code I'm trying to get working. Does someone have a working example of this? Thanks!

#!/usr/bin/perl use warnings; use Daemon::Generic; my $sleeptime = 1; newdaemon( progname => 'ticktockd', pidfile => '/var/run/ticktockd.pid', configfile => '/etc/ticktockd.conf', ); sub gd_preconfig { my ($self) = @_; open(CONFIG, "</etc/ticktockd.conf") or die; while(<CONFIG>) { $sleeptime = $1 if /^sleeptime\s+(\d+)/; } close(CONFIG); return (); } sub gd_run { while(1) { sleep($sleeptime); print scalar(localtime(time))."\n"; } }

Replies are listed 'Best First'.
Re: Generic Daemon Use
by andyford (Curate) on Feb 02, 2007 at 16:34 UTC

    Do you have a "/etc/ticktockd.conf"? Could it be die-ing trying to read a non-existant file? Maybe change

    open(CONFIG, "</etc/ticktockd.conf") or die;
    to
    open(CONFIG, "</etc/ticktockd.conf") or die "Failure opening config fi +le: $!"

    non-Perl: Andy Ford

      Thanks for the reply. I do have the conf file in /etc. I'm getting no error output when using $!.

        Here's another thought: run your Daemon in the foreground:

        newdaemon( progname => 'ticktockd', pidfile => '/var/run/ticktockd.pid', configfile => '/etc/ticktockd.conf', foreground => 1, );
        If it's running in the background you may never see any output.

        non-Perl: Andy Ford

        Well then, I'd say it's probably time to start sprinkling print statements into your code. For instance you might do (untested)

        sub gd_preconfig { my ($self) = @_; print "opening config file\n"; open(CONFIG, "</etc/ticktockd.conf") or die "Failure opening c +onfig file:$!"; while(<CONFIG>) { $sleeptime = $1 if /^sleeptime\s+(\d+)/; } close(CONFIG); print "done reading config, sleeptime is now $sleeptime\n"; return (); }
        Oh, and I just noticed that you need to add "use strict" to the top of your program! Very important!

        non-Perl: Andy Ford

Re: Generic Daemon Use
by Zaxo (Archbishop) on Feb 03, 2007 at 01:21 UTC

    A daemon does not have a controlling terminal, so your print statements should go to a particular file handle. You may not see a print to STDOUT unless you note what device you wish to display on and copy its handle.

    After Compline,
    Zaxo

Re: Generic Daemon Use
by Khen1950fx (Canon) on Feb 03, 2007 at 09:02 UTC
    This worked for me. I used 'apt' instead:

    #!/usr/bin/perl use strict; use warnings; use Daemon::Generic; my $sleeptime = 1 newdaemon( progname => 'apt', pidfile => '/var/run/apt.conf.pid', configfile => '/etc/apt.conf', ); sub gd_preconfig { my ($self) = @_; open(CONFIG, "</etc/apt.conf") while <CONFIG>; close(CONFIG); return (); } sub gd_run { while (1) { sleep($sleeptime); print scalar(localtime(time))."\n"; } }
    Note: I didn't use foreground. It went into an infinite loop. Use: perl myscript.pl start, then perl myscript.pl check. It'll give a pid number.

      I would have thought you'd want your daemon to run in an infinite loop (while waiting for Unix signals/sockets/alarms)

      Otherwise you've just got a normal perl script ...

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;