On Sep 13, 2006, at 3:50 PM, John W. Krahn wrote:
James Marks wrote:
If I've correctly interpreted your suggested changes, the script now
reads:
-- SCRIPT ------------------
#!/usr/bin/perl
use warnings;
use strict;
my $log_file = '/home/james/httpsd_mysqld.log';
open FILE_OUT, ">> $log_file"
or die "Cannot open log file: $!";
select FILE_OUT;
my ( $second, $minute, $hour, $day, $month, $year ) = localtime;
my $time = sprintf '%04d-%02d-%02d %02d:%02d:%02d',
$year + 1900, $month + 1, $day, $hour, $minute, $second;
open PS, '-|', '/bin/ps', 'aux' or die "Cannot open pipe from ps:
$!";
my ( $httpsd_count, $mysqld_count );
while ( <PS> ) {
$httpsd_count++ if /httpsd/;
$mysqld_count++ if /mysqld/;
}
close PS or warn $! ? "Error closing ps pipe: $!"
: "Exit status $? from ps";
print $time,
' httpsd ', $httpsd_count || 'NOT RUNNING',
' mysqld ', $mysqld_count || 'NOT RUNNING',
"\n";
-- END SCRIPT ------------------
The line:
open PS, '-|', '/bin/ps', 'aux' or die "Cannot open pipe from ps:
$!";
returns an error on my Perl 5.6.1 machine (which I can't upgrade):
"Can't use an undefined value as filehandle reference"
Perl's warning/error messages usually end with the file name and
line number.
Are you sure that that is the line the error is referring to?
Yes. I omitted the line number for brevity (sorry) but it was that line.
even though "Programming Perl" says it should work in 5.6.1. (FTR, it
works fine in Perl 5.8.7)
Any ideas as to why that might be?
According to the documentation there are four ways you could write
that:
open PS, '-|', '/bin/ps', 'aux' or die "Cannot open pipe from ps: $!";
open PS, '-|' or exec '/bin/ps', 'aux' or die "Cannot open pipe
from ps: $!";
open PS, '-|', '/bin/ps aux' or die "Cannot open pipe from ps: $!";
open PS, '/bin/ps aux |' or die "Cannot open pipe from ps: $!";
And the last one should work with any version of Perl on any
platform so see
if one of the other ways will work for you. (They all work fine
for me on Linux.)
What turned out to work — although I haven't figured out why yet — is
to to use 'acx' rather than 'aux' and to include that within the
single quotes as in:
open PS, '-|', '/bin/ps acx' or die "Cannot open pipe from ps: $!";
The above line results in the script running fine from cron.
If I don't enclose the ps command with full path and options in the
single quotes as in '/bin/ps acx' the script returns an error.
If I use 'aux' instead of 'acx' the result is a '1' or 'NOT RUNNING'
for the process count.
- James
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>