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

Hello monks! I am trying to write a perl script that in this case just checks to see if nscd is running and does nothing if it is and when it dies to do something else..pretty simple right? Well not for me for some reason? Can some one tell me what is going wrong? Here is my script:

$RUNDBCOLD = nscd;
print "$RUNDBCOLD\n";
while ($RUNDBCOLD == (`ps -ef | grep -v grep | grep "nscd" | awk -F/ '{print \$4}'`))
{
print "job is still running\n";
`/bin/sleep 2`;
}
print "$RUNDBCOLD finished\n";

Edit, BazB: changed title.

Replies are listed 'Best First'.
Re: Checking a command is running
by dragonchild (Archbishop) on Oct 14, 2003 at 19:59 UTC

    First, what are the errors you see? I know of at least two ones ...

    Why don't you just write this in sed or awk or some other shell scripting language? I mean, that's basically what your Perl script is ...

    If you wanted to write this in Perl, I'd do something like the following:

    use strict; use warnings; my $app = 'nscd'; print "Looking for '$app'\n"; while (1) { open PS, "ps -ef |" || die "Cannot open ps command: $!\n"; my @cmd = grep { /$app/ } <PS>; close PS; last unless @cmd; print "'$app' is still running ...\n"; sleep 2; } print "'$app' is finished.\n";

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Checking a command is running
by pzbagel (Chaplain) on Oct 14, 2003 at 19:59 UTC

    The first problem is that you've written a shell script in Perl. Perl can do all those things: grep, awk, sleep without all the backticks. And if you are adventuresome, you can use a module to replace the functionality of ps too.

    Now onto your actual bug. You need to use "eq" to do your string comparison. "==" does a numerical comparison and since neither string has any numbers in it you get "0 == 0" and therefore it's true.

    while ($RUNDBCOLD eq (`ps -ef | grep -v grep | grep "nscd" | awk -F/ ' +{print \$4}'`))

    HTH

Re: Checking a command is running
by mrbbq (Sexton) on Oct 14, 2003 at 20:20 UTC
    Thanks everyone! Here is how i did it:

    $RUNDBCOLD = `ps -ef | grep -v grep | grep "nscd" | awk -F/ '{print \$4}'`;
    print "$RUNDBCOLD\n";
    while ($RUNDBCOLD ) {
    print "job is still running\n";
    `/bin/sleep 2`;
    $RUNDBCOLD = `ps -ef | grep -v grep | grep "nscd" | awk -F/ '{print \$4}'`;
    }
    print "$RUNDBCOLD finished\n";

      Glad you figured it out!

      Here's another idea - just something to think about. On my system (Red Hat Linux 9) there's an 'nscd' start/stop/status script in /etc/rc.d/init.d. So at a command prompt I can do

      service nscd status
      and if the nscd daemon is running, the return code will be zero(0) - if it's not running, the return code will be some positive number. So, you could do something like this:
      #!/usr/bin/perl -w @args = ("/sbin/service", "nscd", "status"); while (system(@args) == 0) { print "job still running!\n"; sleep 2; }
      HTH.

      As other posters have pointed out, Perl isn't shell, although it can be used that way.

      You make calls, via backticks (``) to external commands - even to call text processing tools such as awk, when you could do most of it internally with perl.

      There are modules on CPAN that allow you to look at the process list on UNIX systems (see Proc::ProcessTable)

      dragonchild has give you a good example - you should work through the way his example works and learn from it.

      Cheers

      BazB


      If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
      That way everyone learns.