Reputation: 60
I am trying to find the processes which are not running through perl. It works for some processes using following code but not for cgred service.
foreach $critproc (@critarray)
{
#system("/usr/bin/pgrep $critproc");
$var1=`/usr/bin/pgrep $critproc`;
print "$var1";
print "exit status: $?\n:$critproc\n";
if ($? != 0)
{
$probs="$probs $critproc,";
$proccrit=1;
}
}
For cgred
I have to specify /usr/bin/pgrep -f cgred
to check whether any pid is associated with it or not.
But when I specify -f
in above code it gives exit status 0
($?
) to all the processes even if its not running.
Can you anyone tell me how to pass arguments to unix command in Perl.
Thanks
Upvotes: 0
Views: 55
Reputation: 386461
What's $critproc
? Where's the -f
you say is giving you problems? One might imagine you have some kind of escaping problem, but that shouldn't be the case if $critproc
is cgred
as you seem to imply.
Given these problem, I'm just going to answer the general question.
The following avoids the shell, so no need to build a shell command:
system("/usr/bin/pgrep", "-f", $critproc);
die "Killed by signal ".( $? & 0x7F ) if $? & 0x7F;
die "Exited with error ".( $? >> 8 ) if ($? >> 8) > 1;
my $found = !($? >> 8);
If you need a shell command, you can use String::ShellQuote's shell_quote
to build it.
use String::ShellQuote qw( shell_quote );
my $shell_cmd = shell_quote("/usr/bin/pgrep", "-f", $critproc) . " >/dev/null";
system($shell_cmd);
die "Killed by signal ".( $? & 0x7F ) if $? & 0x7F;
die "Exited with error ".( $? >> 8 ) if ($? >> 8) > 1;
my $found = !($? >> 8);
or
use String::ShellQuote qw( shell_quote );
my $shell_cmd = shell_quote("/usr/bin/pgrep", "-f", $critproc);
my $pid = `$shell_cmd`;
die "Killed by signal ".( $? & 0x7F ) if $? & 0x7F;
die "Exited with error ".( $? >> 8 ) if ($? >> 8) > 1;
my $found = !($? >> 8);
Upvotes: 4