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

How does one verify whether a daemon is alive using Perl ? Using the Bourne shell I code :
ps -ef | grep -v grep | grep inetd $rc = $? if [ $rc -ne 0 ] ; then echo " inetd is not up " fi
Using Perl, how does one test the return value of the ps + grep commands ?? TIA

Replies are listed 'Best First'.
Re: Test for daemon
by robartes (Priest) on Oct 08, 2002 at 20:52 UTC
    The ugly way is to use system, as in:
    use strict; my $rc=system("ps -ef | grep [i]netd"); if ($rc) {print "inetd seems to have gone the way of the dodo.\n"};
    The nice, clean way is to use Proc::ProcessTable, as in:
    use strict; use Proc::ProcessTable; my $proctable=Proc::ProcessTable->new; my $proclist= $proctable->table; my @result=map {/(inetd)/} @$proclist; unless (scalar(@result)) {print "inetd->walk({direction=>dodo})\n"};
    Warning: the above code is untested. It could work, but it also could wipe your hard drive, send mail professing your never ending devotion to everyone you have ever mailed to, paint your cats green and your house orange and make a camel crawl through the eye of a needle. You have been warned.

    CU
    Robartes-

    Update:Got rid of the grep -v grep I mindlessly copy/pasted into the code. Thanks to merlyn for pointing out this important issue.

      ps -ef | grep -v grep | grep inetd
      Please, let us not cargo-cult this crazy "grep -v grep" some more. I cringe each time I see that. It's a poor solution to a common problem.

      The way to avoid picking out the grep process itself is to ask ps for the proper things:

      ps axc | grep inetd
      If your ps doesn't have a "command-name only" solution, then simply muddy the argument so that the grep cannot match it:
      ps ax | grep '[i]netd'
      Let's not be wasting processes needlessly. Think before you type, people.

      -- Randal L. Schwartz, Perl hacker

      Another quasi ugly system call could be something like
      use strict; my($ps_cmd,$found); $ps_cmd = '/bin/ps -auwx'; open(IN,"$ps_cmd|") || die "Couldnt exec $ps_cmd\nReason: $!\n"; while ( <IN> ) { ($found++ && last) if ($_ =~ /inetd/); } print STDERR "Danger Will Robinson\n" if (!$found);
      Also below there was a post about collecting the ps output into an array, and then grep()'ing inetd from the output, which works nicely too...
      P.S. Code tested clean on FreeBSD

      Minor nit: the questioner was using grep, which is still a nice way to do it:

      unless (grep(/\binetd\b/, @$proclist)) { print "inetd's not there\n"; }
      I'm not familiar with the module, so I don't know if you need the \b in the pattern or not, but it shouldn't hurt.

      Untested code, etc., etc.

Re: Test for daemon
by kabel (Chaplain) on Oct 08, 2002 at 20:39 UTC
    i have no unix box available, but check if inetd puts a file inside the /var/run directory named inetd.pid or so. then check if this pid still exists.

    another way: try to gather some data from one of the programs which are invoked via inetd. if it is possible, inetd must be alive.
Re: Test for daemon
by Rex(Wrecks) (Curate) on Oct 08, 2002 at 20:47 UTC
    Update: The example below is much better than using system(), please use it as a reference instead. And ++ to robartes :)

    Look up system(), it will give you the output or the return value of 'ps' and or 'grep'. Personaly, I would recomend using system for the 'ps' and then using perl to grep through the output to fine what you want.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Test for daemon
by BazB (Priest) on Oct 08, 2002 at 21:23 UTC

    If you want to check to see if a network server/daemon is running and actually responding to queries, it might be worthwhile using something like Net::Ping to ping the relevant port on whatever host is running the service.

    You could always combine this with using Proc::ProcessTable as robartes suggested.

    Of course, if it's not a network service, my suggestion is a bit redundant :-)

    Cheers.

    BazB

Re: Test for daemon
by wube (Acolyte) on Oct 10, 2002 at 18:49 UTC
    Thanks to all who replied. You have given me enough ideas and direction to get me started.