On Sep 12, 2006, at 11:02 PM, Mumia W. wrote:

On 09/12/2006 11:28 PM, James Marks wrote:
Hi folks,
I don't know if this is a Perl or UNIX problem and I'm hoping you can help me figure that out. I wrote a script that checks to see if the httpsd and mysqld processes are running on my server and to log the results of those tests. When I run the script from the command line, the script prints to a log file the number of each of those processes that are running at the time of the script execution. When I run the same script from cron, however, it only prints a "1" for httpsd and mysqld.
[...]
my $httpsd_count = `ps -aux | grep -c httpsd`;
my $mysqld_count = `ps -aux | grep -c mysqld`;
[...]
Any ideas on why the same script has different results when run from the command line vs. from cron?
Thanks,
James

I don't know if this would affect the output, but "ps -aux" gives me this in addition to the normal output: "Warning: bad syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html";

Perhaps "ps hcax" would suffice.

Also, watch what the "grep -c ..." is doing. Perhaps the counting isn't going as planned. I would count from within Perl.

This is how I might write the script:

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

local $\ = "\n";
local $, = " ";
my $ps = `ps hcax`;
my $logfile = '/tmp/counts-ma.log';
my $tty = `tty`;
chomp $tty;
my $mysqlc = () = $ps =~ m/\bmysqld\b/g;
my $httpdc = () = $ps =~ m/\bapache2\b/g;

open (STDOUT, '>>', $logfile) or die("Couldn't write to
$logfile\n");
my $date = `date +"%Y-%m-%d %H:%M:%S"`;
chomp $date;
print $date, httpd => $httpdc, mysql => $mysqlc;

Thanks, Mumia. That seems to work both from the command line and from cron. (I'm not sure what purpose $tty serves. I took it out and it runs fine without it.)

I'm still baffled as to why the original script runs differently when run by cron rather than from the command line. Can anybody explain that?

James


--
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