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.


In reply to Re^2: Running multiple instances of a daemon simultaneously? by Trenin
in thread Running multiple instances of a daemon simultaneously? by Trenin

Title:
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, details, 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, summary, 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.