On Sep 13, 2006, at 12:29 AM, John W. Krahn wrote:
#!/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";
Thanks John. I'll need to wait until morning to study this.
Any ideas on why the same script has different results when run from
the
command line vs. from cron?
I tested your script from cron and from the command line and didn't
see a
difference.
That's interesting. I got a distinctly different output every time when
running it from the command line compared to running it in cron. The
output from cron didn't appear to include a count of running processes.
Thanks again,
James
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>