1828586 Members
2623 Online
109982 Solutions
New Discussion

Script Help

 
SOLVED
Go to solution
Sivasingam Santhakumar
Frequent Advisor

Script Help

Hi gurus,

I can't see any problem in my below perl script. There are 4 rsyn deamons. My script to check if 4
daemons are running.

#!/bin/perl
my $CMD;
$CMD=system("ps -ef |grep -v grep |grep rsync |wc -l");
if ($CMD != 4)
{
print "There should 4 rsync daemons running, only x process running\n";
}

But the script prints message even there are 4 daemons running. I know it's a basic and simple script but I am missing something.
Can someone point me the problem?. System is RH9.

Thanks
Siva
3 REPLIES 3
Stuart Browne
Honored Contributor
Solution

Re: Script Help

What you're doing here is getting the handle on a system process, not on the output of a given command. To do that, you need to open the command in a pipe, similar to:

open( FH, "ps -ef | grep -v grep | grep rsync | wc -l |" );
$output = ;
if ($output != ) {
print "blah\n";
}

-- (Be aware, that 'wc -l' outputs spaces/tabs infront of your number, so you'd need to clean it up a fair bit first, or possibly just use 'ps -ef | grep -v grep | grep -c rsync' instead).

Although if you're going to be doing something like this, why use a perl script?

As a simple shell script, something like:

#!/bin/bash
if [ "$(ps -ef | grep -v grep | grep -c rsync)" -ne 4 ]
then
echo "There should be 4"
fi

Just some thoughts.
One long-haired git at your service...
Sivasingam Santhakumar
Frequent Advisor

Re: Script Help

Stuart,

Thanks for the quick one and the explanation. I need to use perl because of the email function.

Thanks again
Siva
Stuart Browne
Honored Contributor

Re: Script Help

Fair enough.

With a little playing though, you can get email out of shell scripts just as easily (pipe the body of a message into the 'mail' command *shrug*).

If you're happy using perl though, stick with it! It's truely a fabulous language!
One long-haired git at your service...