import-bot (20211) [Avatar] Offline
#1
[Originally posted by fjohn]

Hello Andrew,
I am attempting to evaluate the status of a process using the open
function. My objective is to determine whether the process, in this case, vold
is running or has stopped. If it stopped, I want to restart it. My current
code only checks for the existance on the command, ps and reports that it is
successful, and not on the status of vold. I think I have to check on the exit
status?, but I am not clear on how I would perform this operation. Your help
would be greatly appreciated.

$thishost='/bin/uname -n';
chop($thishost);

open(IN, "/usr/bin/ps -ef | grep vold |") die "can't open process: $!";
while ($process = <IN>smilie {
if ($process = 0) {
print "The process $process on $thishost is running
";
} else {
print "The process $process has stopped on $thishost
";
}
}
import-bot (20211) [Avatar] Offline
#2
Re: Filehandles
[Originally posted by jandrew]


Well, you are piping the output of ps through grep and reading the
results line by line -- but testing if the line read = 0 (with an
assignment = rather than a numerical == comparison, or a string eq
comparison).

I'm thinking you'd rather just want to know if any lines at all
are available to read -- ie, any line read in from the grep would
contain 'vold' and thus the process is running. In fact, for this
you might just want backticks to run the process and capture the
output -- for example, to see how many 'bash' processes are running
on my machine at the moment:

my $process = 'bash';
my @is_up = `ps |grep $process`;
my $count = @is_up;
if($count){
print "You have $count instances of '$process' running
";
} else {
print "'$process' is not running right now
";
}
__END__

Which prints:

You have 9 instances of 'bash' running

Does that help with your problem at all?

regards,
andrew
import-bot (20211) [Avatar] Offline
#3
Re: Filehandles
[Originally posted by jandrew]


> Ummm? Does it have to do with the program considering the grep
> statement an instance of the process? Thanks.

Uhmm, that might be a problem (though I don't see it on my machine).

Maybe you should just get the output from ps into an array and then
grep through that yourself (with perl's 'grep') ... then you'll know
you aren't accidentally hitting the grep vold in the ps table:

my @running = `ps -ef`;
my $process = 'bash';
my $count = grep /$process/, @running;
if ($count) {
print "You have $count instances of '$process' running
";
}

andrew
import-bot (20211) [Avatar] Offline
#4
Re: Filehandles
[Originally posted by fjohn]

>
> > Ummm? Does it have to do with the program considering the grep
> > statement an instance of the process? Thanks.
>
> Uhmm, that might be a problem (though I don't see it on my machine).
>
> Maybe you should just get the output from ps into an array and then
> grep through that yourself (with perl's 'grep') ... then you'll know
> you aren't accidentally hitting the grep vold in the ps table:
>
> my @running = `ps -ef`;
> my $process = 'bash';
> my $count = grep /$process/, @running;
> if ($count) {
> print "You have $count instances of '$process' running
";
> }
>
> andrew
Yes, that worked. It correctly read the ps -ef and only reports running if it
sees the corresponding name of the process I specified. Hey Thanks!smilie

fred
import-bot (20211) [Avatar] Offline
#5
Re: Filehandles
[Originally posted by fjohn]

>
> Well, you are piping the output of ps through grep and reading the
> results line by line -- but testing if the line read = 0 (with an
> assignment = rather than a numerical == comparison, or a string eq
> comparison).
>
> I'm thinking you'd rather just want to know if any lines at all
> are available to read -- ie, any line read in from the grep would
> contain 'vold' and thus the process is running. In fact, for this
> you might just want backticks to run the process and capture the
> output -- for example, to see how many 'bash' processes are running
> on my machine at the moment:
>
> my $process = 'bash';
> my @is_up = `ps |grep $process`;
> my $count = @is_up;
> if($count){
> print "You have $count instances of '$process' running
";
> } else {
> print "'$process' is not running right now
";
> }
> __END__
>
> Which prints:
>
> You have 9 instances of 'bash' running
>
> Does that help with your problem at all?
>
> regards,
> andrew

Hello Andrew,
Well, I think I'm on the right track, although I'm still getting a report
of a process running, when in fact the agent that initiates the process has
never been compiled and installed on the machine. Also, when I use ps -ef |
grep on the console for the specific process I am concerned about, it reports
back with nothing. Ummm? Does it have to do with the program considering the
grep statement an instance of the process? Thanks.

fred