Command line vs. cron

2006-09-12 Thread James Marks

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.




script output to log when run from the command line:

2006-08-12 21:10:20 httpsd 23 mysqld 33



script output to log when run from cron:

2006-08-12 21:11:05 httpsd 1 mysqld 1



Here's the cron entry:

*/15*   *   *   *   perl 
/home/james/code/cron_code/httpsd_mysqld_check.pl




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


printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day, 
$hour, $minute, $second);


my $httpsd_count = `ps -aux | grep -c httpsd`;
my $mysqld_count = `ps -aux | grep -c mysqld`;

chomp($httpsd_count);
chomp($mysqld_count);

my $httpsd_msg = '';
my $mysqld_msg = '';

if ($httpsd_count > 0) {
print "httpsd $httpsd_count ";
}
else {
print "httpsd NOT RUNNING ";
}

if ($mysqld_count > 0) {
print "mysqld $mysqld_count ";
}
else {
print "mysqld NOT RUNNING ";
}

print "\n";

close FILE_OUT;



Any ideas on why the same script has different results when run from 
the command line vs. from cron?


Thanks,

James


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread Owen Cook

On Tue, 12 Sep 2006, 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.


> 
> 
> 
> Here's the cron entry:
> 
> */15*   *   *   *   perl 
> /home/james/code/cron_code/httpsd_mysqld_check.pl
> 
> 
> 
> 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];
> 
> printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day, 
> $hour, $minute, $second);
> 
> my $httpsd_count = `ps -aux | grep -c httpsd`;
> my $mysqld_count = `ps -aux | grep -c mysqld`;
> 
> chomp($httpsd_count);
> chomp($mysqld_count);
> 
> my $httpsd_msg = '';
> my $mysqld_msg = '';
> 
> if ($httpsd_count > 0) {
>  print "httpsd $httpsd_count ";
> }
> else {
>  print "httpsd NOT RUNNING ";
> }
> 
> if ($mysqld_count > 0) {
>  print "mysqld $mysqld_count ";
> }
> else {
>  print "mysqld NOT RUNNING ";
> }
> 
> print "\n";
> 
> close FILE_OUT;
> 


I suspect that your env includes the path to "ps"

For cron you would need the full path

Try 
 my $httpsd_count = `/usr/bin/ps -aux | grep -c httpsd`;
 my $mysqld_count = `/usr/bin/ps -aux | grep -c mysqld`;

or wherever ps is



Owen


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread James Marks


On Sep 12, 2006, at 9:59 PM, Owen Cook wrote:


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

printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day,
$hour, $minute, $second);

my $httpsd_count = `ps -aux | grep -c httpsd`;
my $mysqld_count = `ps -aux | grep -c mysqld`;

chomp($httpsd_count);
chomp($mysqld_count);

my $httpsd_msg = '';
my $mysqld_msg = '';

if ($httpsd_count > 0) {
 print "httpsd $httpsd_count ";
}
else {
 print "httpsd NOT RUNNING ";
}

if ($mysqld_count > 0) {
 print "mysqld $mysqld_count ";
}
else {
 print "mysqld NOT RUNNING ";
}

print "\n";

close FILE_OUT;




I suspect that your env includes the path to "ps"

For cron you would need the full path

Try
 my $httpsd_count = `/usr/bin/ps -aux | grep -c httpsd`;
 my $mysqld_count = `/usr/bin/ps -aux | grep -c mysqld`;


I just tried that and let it run in cron but there's no difference in 
output. I still get "1"s in cron and process counts from the command 
line.


cron:
2006-08-12 22:15:20 httpsd 1 mysqld 1

command line:
2006-08-12 22:19:18 httpsd 19 mysqld 30


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread Owen Cook

On Tue, 12 Sep 2006, James Marks wrote:

> 
> On Sep 12, 2006, at 9:59 PM, Owen Cook wrote:
> 
> >> 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];
> >>
> >> printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day,
> >> $hour, $minute, $second);
> >>
> >> my $httpsd_count = `ps -aux | grep -c httpsd`;
> >> my $mysqld_count = `ps -aux | grep -c mysqld`;
> >>
> >> chomp($httpsd_count);
> >> chomp($mysqld_count);
> >>
> >> my $httpsd_msg = '';
> >> my $mysqld_msg = '';
> >>
> >> if ($httpsd_count > 0) {
> >>  print "httpsd $httpsd_count ";
> >> }
> >> else {
> >>  print "httpsd NOT RUNNING ";
> >> }
> >>
> >> if ($mysqld_count > 0) {
> >>  print "mysqld $mysqld_count ";
> >> }
> >> else {
> >>  print "mysqld NOT RUNNING ";
> >> }
> >>
> >> print "\n";
> >>
> >> close FILE_OUT;
> >>
> >
> >
> > I suspect that your env includes the path to "ps"
> >
> > For cron you would need the full path
> >
> > Try
> >  my $httpsd_count = `/usr/bin/ps -aux | grep -c httpsd`;
> >  my $mysqld_count = `/usr/bin/ps -aux | grep -c mysqld`;
> 
> I just tried that and let it run in cron but there's no difference in 
> output. I still get "1"s in cron and process counts from the command 
> line.

Ooops, how about the full path to grep as well!

 my $httpsd_count = `/usr/bin/ps -aux | /usr/bin/grep -c httpsd`;
 my $mysqld_count = `/usr/bin/ps -aux | /usr/bin/grep -c mysqld`;


Owen


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread James Marks


On Sep 12, 2006, at 10:28 PM, Owen Cook wrote:


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

printf("%04s-%02s-%02s %02d:%02d:%02d ", $year+1900, $month, $day,
$hour, $minute, $second);

my $httpsd_count = `ps -aux | grep -c httpsd`;
my $mysqld_count = `ps -aux | grep -c mysqld`;

chomp($httpsd_count);
chomp($mysqld_count);

my $httpsd_msg = '';
my $mysqld_msg = '';

if ($httpsd_count > 0) {
 print "httpsd $httpsd_count ";
}
else {
 print "httpsd NOT RUNNING ";
}

if ($mysqld_count > 0) {
 print "mysqld $mysqld_count ";
}
else {
 print "mysqld NOT RUNNING ";
}

print "\n";

close FILE_OUT;




I suspect that your env includes the path to "ps"

For cron you would need the full path

Try
 my $httpsd_count = `/usr/bin/ps -aux | grep -c httpsd`;
 my $mysqld_count = `/usr/bin/ps -aux | grep -c mysqld`;


I just tried that and let it run in cron but there's no difference in
output. I still get "1"s in cron and process counts from the command
line.


Ooops, how about the full path to grep as well!

 my $httpsd_count = `/usr/bin/ps -aux | /usr/bin/grep -c httpsd`;
 my $mysqld_count = `/usr/bin/ps -aux | /usr/bin/grep -c mysqld`;


Tried adding the full path to grep and it just gets worse:

my $httpsd_count = `/bin/ps -aux | /usr/bin/grep -c httpsd`;
my $mysqld_count = `/bin/ps -aux | /usr/bin/grep -c mysqld`;

(on my machine, ps is in /bin)

cron:
2006-08-12 22:30:20 httpsd NOT RUNNING mysqld NOT RUNNING
2006-08-12 22:32:20 httpsd NOT RUNNING mysqld NOT RUNNING
2006-08-12 22:34:20 httpsd NOT RUNNING mysqld NOT RUNNING

command line:
2006-08-12 22:34:41 httpsd 20 mysqld 31



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread Mumia W.

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;




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-12 Thread James Marks


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]
 




Re: Command line vs. cron

2006-09-13 Thread John W. Krahn
James Marks wrote:
> Hi folks,

Hello,

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

cron has only a minimal environment.


> script output to log when run from the command line:
> 2006-08-12 21:10:20 httpsd 23 mysqld 33
> 
> script output to log when run from cron:
> 2006-08-12 21:11:05 httpsd 1 mysqld 1
> 
> Here's the cron entry:
> */15*   *   *   *   perl
> /home/james/code/cron_code/httpsd_mysqld_check.pl

There is no need to put 'perl' in there because the first line of your script
is '#!/usr/bin/perl'.


> 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 (  ) {
$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";


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



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-13 Thread James Marks


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 (  ) {
$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]
 




Re: Command line vs. cron

2006-09-13 Thread Travis Thornhill
I was just looking into the %ENV hash in my trusty Programming Perl book
  and found this interesting note on p. 661:
"Note that processes running as crontab(5) entries inherit a particularly 
impoverished set of environment variables. (If your program runs fine from the 
command line but not under cron, this is probably why.)"
   
  - Travis
  .
   
   
   
  James Marks <[EMAIL PROTECTED]> wrote:
  
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]






-
 All-new Yahoo! Mail - Fire up a more powerful email and get things done faster.

Re: Command line vs. cron

2006-09-13 Thread James Marks


On Sep 13, 2006, at 1:01 AM, Travis Thornhill wrote:

I was just looking into the %ENV hash in my trusty Programming Perl 
book

  and found this interesting note on p. 661:
"Note that processes running as crontab(5) entries inherit a 
particularly impoverished set of environment variables. (If your 
program runs fine from the command line but not under cron, this is 
probably why.)"


  - Travis


Thanks, Travis. I'll look into that to get a better understanding of 
the cron environment.


James


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-13 Thread James Marks

(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 (  ) {
$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 (  ) {
$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]
 




Re: Command line vs. cron

2006-09-13 Thread John W. Krahn
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 (  ) {
> $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?


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

Check the docs:

perldoc perlport
perldoc perl

and they should have information on supported and unsupported features.




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Command line vs. cron

2006-09-13 Thread James Marks


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 (  ) {
$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]
 




Re: Command line vs. cron

2006-09-13 Thread Igor Sutton

There's some reason to not use Proc::ProcessTable? It is really easy to use,
and doesnt' relies on environment variables to be used.

--
Igor Sutton Lopes
t: +55 51 9627.0779
e: [EMAIL PROTECTED]


Re: Command line vs. cron

2006-09-13 Thread Mumia W.

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]