in reply to Test for daemon

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.


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

Replies are listed 'Best First'.
Say "no" to "grep -v grep"
by merlyn (Sage) on Oct 08, 2002 at 21:25 UTC
    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

Re: Re: Test for daemon
by l2kashe (Deacon) on Oct 08, 2002 at 23:11 UTC
    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
Re: Re: Test for daemon
by VSarkiss (Monsignor) on Oct 08, 2002 at 21:22 UTC

    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.