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

Hi guys, I'm fairly new to perl. I've been looking into creating daemons, but the thing is I want to be able to run multiple instances of this same daemon simultaneously, which seems to be the opposite of the goal every example I've come across. Practical Proc::Daemon example pretty clearly lays out how to create a daemon , but it's specifically coded to only have one running at a time, and I can't figure out how to change it without breaking it.

Ideally I'd like to be able to start a daemon:

perl daemon.pl [options]

perl daemon.pl [different options]

And then they'd perform their respective tasks independent of each other, and I'd just kill them according to their respective PIDs if need be.

Is there any way to perform this?

Thanks.
  • Comment on Running multiple instances of a daemon simultaneously?

Replies are listed 'Best First'.
Re: Running multiple instances of a daemon simultaneously?
by roboticus (Chancellor) on Aug 06, 2014 at 17:57 UTC

    Trenin:

    I just reviewed the docs for Proc::Daemon and don't see where the difficulty lies in running multiple instances. What's giving you the problem?

    As you mention, the code in the link provided does want to use a single daemon, though it doesn't look like it would be all that difficult to modify. To do so, I'd suggest adding a PID command line argument to tell the daemons apart, and make sure the run function displays the pid of the new daemon. Finally, if the PID argument passed in was 0 or LIST or some such, it would just list all matching daemons.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Yeah I guess I just wasn't firing on all cylinders yesterday; I'm not sure why I couldn't get it to work. I've run into a slightly different issue: the daemon doesn't return input control to the user. So for example the behavior of the script I borrowed from is:

      [user@linux ~] perl original.pl --start

      Starting...

      [user@linux ~]

      But my version hangs, like

      [user@linux ~] perl original.pl --start

      Starting...

      and I have to ctrl-z before I can enter anything else

      Here's my modified example daemon:

      #!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Proc::Daemon; use Cwd; use File::Spec::Functions; my $daemon = Proc::Daemon->new( work_dir => getcwd() ); my $pid; my $daemonize = 1; GetOptions( 'daemon!' => \$daemonize, "start" => \&run, "status" => \&status, "stop" => \&stop ); sub stop{ if($pid){ print "Stopping pid $pid... \n"; if($daemon->Kill_Daemon($pid)){ print "Successfully stopped. \n"; }else{ print "Could not find $pid. Was it running? \n"; } }else{ print "not Running, nothing to stop. \n"; } } sub status{ if($pid){ print "Running with pid $pid. \n"; }else{ print "Not running. \n"; } } sub run{ if (!$pid){ print "Starting...\n"; if($daemonize){ $pid = $daemon->Init; } while(1){ open(my $FH, '>>', catfile(getcwd(), "log.txt")); print $FH "Logging from PID $pid at " . time() . "\n"; close $FH; sleep 5; } }else{ print "No active daemons. \n"; } }
      I've narrowed the problem down to the assignment $pid = $daemon->Init;

      But removing the assignment obviously means the pid isn't stored. I guess I could just go with command line arguments.

      EDIT: OR I could write a temp pid file, write to and then read from the file, and then destroy it.

        Trenin:

        Are you certain that it's $daemon->Init? The reason I ask is that immediately after doing that you enter an infinite loop, so I wouldn't expect you to get control again until you either put the job in the background or terminate it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Running multiple instances of a daemon simultaneously?
by Anonymous Monk on Aug 06, 2014 at 16:21 UTC

    One idea might be to require the location of the PID file on the command line.

    daemon.pl -p /var/run/task1.pid ... daemon.pl -p /var/run/task2.pid ...
      Thanks for the suggestion; I may go with this.
Re: Running multiple instances of a daemon simultaneously?
by afoken (Chancellor) on Aug 10, 2014 at 08:46 UTC
      Awesome, thanks for all of the links and information.