Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to verify if a daemon is up and running ?

by wube (Acolyte)
on Dec 03, 2002 at 20:18 UTC ( [id://217334]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks. How does one test whether a daemon is up and running ?? My code follows :
my $rc_testd = grep /testd/ , ( `ps -ef` ) ; if ($rc_testd > 0) { print "testd daemon is up and running \n" ; exit 1 ; }
The snippet is in a script called " testd.pl" . The problem is that this code finds itself running and always exits. What I really want to do is to see if a previous invocation is still active ?? Any advice will me much appreciated. TIA

Replies are listed 'Best First'.
Re: How to verify if a daemon is up and running ?
by rob_au (Abbot) on Dec 03, 2002 at 20:53 UTC
    I would recommend the usage of Proc::ProcessTable which I have reviewed previously on this site here. Using Proc::ProcessTable, your code would look something like this:

    use Proc::ProcessTable; my $running = 0; my $proc = Proc::ProcessTable->new; foreach ( @{ $proc->table } ) { if ($_->cmndline =~ /testd/) { ++$running; } } if (!$running) { # do something to reinvoke process }

    This code will count the number of testd processes running on the system, returned in $running, which should allow you to determine whether this existing process should continue execution.

    The other approach to this problem would be to incorporate a process identification or lock file, as suggested by Ryszard above, which can be monitored external the current execution process.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000000111110001"))'

Re: How to verify if a daemon is up and running ?
by Ryszard (Priest) on Dec 03, 2002 at 20:37 UTC
    The decent method for you to do this is to create a lock file...

    On startup your prgy would read in a pid, and do a grep in the process list for that pid (and perhaps the name of your prgy ( to prevent collisions with reused pids)). If it finds the pid, it exits, if not, it continues and writes out its own pid ($$).

    This is a pretty standard method to prevent dupe processes...

Re: How to verify if a daemon is up and running ?
by ehdonhon (Curate) on Dec 04, 2002 at 03:49 UTC
Re: How to verify if a daemon is up and running ?
by Monky Python (Scribe) on Dec 04, 2002 at 09:03 UTC
    Hi,
    the "quick and dirty" solution to your problem description.
    my $rc_testd = grep /testd/ , ( `ps -ef` ) ; if ($rc_testd > 1) { print "testd daemon is up and running \n" ; exit 1 ; }
    But it only works if you have exacly one running "testd.pl".
    For a better approach you should consider the solutions by rob_au or ehdonhon.
    MP
Re: How to verify if a daemon is up and running ?
by iburrell (Chaplain) on Dec 04, 2002 at 01:49 UTC
    If you are going to use system-specific ps flags, you might as go all the way and save the use of grep and parsing the ps output. With the Linux ps, you can get just the pid of the syslogd process with:
    ps -C syslogd -o pid=
    You can remove the current process from the list by removing the current process id in $$.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217334]
Approved by gjb
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found