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

I'm doing a simple script that just doesn't work :) It's _SUPPOSED_ to check to make sure the daemons listed are running, but it always says they are dead.

I'm open to other ways to make this check happen for each of the daemons if anyone has any ideas.

Thanks!

my @loc_daemons = qw( crond winbindd smbd nmbd cups sshd zappa ); sub check_daemons { my ($process, $allcool, $psaxline); my @psax_list = `ps ax`; foreach $process (@loc_daemons) { chomp $process; # Just in case $allcool = 'true'; foreach $psaxline (@psax_list) { if ($psaxline =~ "$process") { $allcool = 'true' unless ($allcool eq +'false'); } else { $allcool = 'false'; } } if ($allcool eq 'false') { $output .= "$process does not appear to be run +ning!\n"; $hellbrokeloose = 'true'; } } }

Replies are listed 'Best First'.
Re: Checking a running daemon
by Kanji (Parson) on Oct 22, 2002 at 22:56 UTC

    The reason unless everything appears dead is once $allcool has been set to false (as it will if any of the processes before or after it don't match $process), there's no way for it to become true again.

    Instead, consider inverting your logic so that your state is always false until proved otherwise, like...

    my $allcool = 0; # start false foreach my $psaxline (@psax_list) { $allcool++ if ($psaxline =~ /$process/); } unless ($allcool) { print "$process does not appear to be running!\n"; }

    ...or even simpler...

    print "$process does not appear to be running!\n" unless grep /$process/, @psax_list;

    ... which does pretty much the same thing, as grep returns only it's matches, so if there aren't any...

        --k.

    Update: markup fixes.


Re: Checking a running daemon
by BrowserUk (Patriarch) on Oct 22, 2002 at 22:50 UTC

    Try changing

    if ($psaxline =~ "$process") {

    to

    if ($psaxline =~ m!$process!) {

    If you want to use the =~ operator, you need to use it in conjunction with a regex.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Checking a running daemon
by kvale (Monsignor) on Oct 22, 2002 at 22:55 UTC
    I think the trouble is in your regexp. Instead of
    if ($psaxline =~ "$process") {
    try
    if ($psaxline =~ /$process/) {

    -Mark

Re: Checking a running daemon
by graff (Chancellor) on Oct 23, 2002 at 00:53 UTC
    my @loc_daemons = qw( ... ); ... foreach $process (@loc_daemons) { chomp $process; # Just in case ...
    "Just in case" what? Do you have doubts about how "qw()" works? You shouldn't, but if you do, and the docs don't convince you, take a couple minutes to test it out. You don't need chomp there.

    Kanji's advice about using grep is the best approach, I think. You need just those last two lines inside your foreach $process (@loc_daemons) loop, and you're done.