On 09/13/2006 06:07 PM, James Marks wrote:

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



#!/usr/bin/perl
use strict;
use warnings;

local ($\, $,) = ("\n", " ");
my $logfile = '/tmp/counts-ma.log';
my $ps = `ps aux`;
my @group = (
    `date +"%Y-%m-%d %H:%M:%S"`,
    httpd => scalar (() = $ps =~ m{/usr/sbin/apache2\b}g),
    mysql => scalar (() = $ps =~ m{/usr/sbin/mysqld\b}g),
    );
chop $group[0];
open (STDOUT, '>>', $logfile) or die("Couldn't write log: $!\n");
print @group;

__END__

It's important to examine the output of "ps" to see what you need to extract, and it's best to let ps give you the smallest, simplest output possible to reduce the chances that you'll extract the wrong things.

For example, when I changed from using "ps hcax" to "ps aux," I got 51 mysql processes returned by my script--which was clearly wrong; my problem was that I was counting the number of "mysqld" strings found, but the "u" option to 'ps' outputs too much data--the entire command lines for all processes, and 'mysqld' appears several times on the command lines of each of the mysqld processes.

Keep it simple.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to