(snip)
Here's the script:
#!/usr/bin/perl
use warnings;
use strict;
my $log_file = '/home/james/code/cron_code/httpsd_mysqld_log_file';
open FILE_OUT, ">> $log_file"
or die "Cannot open log file: $!";
select FILE_OUT;
(my $month, my $day, my $year, my $hour, my $minute, my $second) =
(localtime)[4, 3, 5, 2, 1, 0];
my ( $second, $minute, $hour, $day, $month, $year ) = localtime;
printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day,
$hour, $minute, $second);
The month field starts from 0 so you have to add 1 to it.
my $time = sprintf '%04d-%02d-%02d %02d:%02d:%02d',
$year + 1900, $month + 1, $day, $hour, $minute, $second;
my $httpsd_count = `ps -aux | grep -c httpsd`;
my $mysqld_count = `ps -aux | grep -c mysqld`;
Your count will be off by one because the grep command is included
in the
count. No need to run ps twice and no need for the grep command or
the shell:
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";
John,
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"
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?
James
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>