  | |  | shell script to count httpd processes | shell script to count httpd processes 2005-03-14 - By Michael Velez
Back
> -- --Original Message-- --
> From: redhat-list-bounces@(protected)
> [mailto:redhat-list-bounces@(protected)] On Behalf Of Steve Buehler
> Sent: Monday, March 14, 2005 2:02 PM
> To: redhat-list@(protected)
> Subject: shell script to count httpd processes
>
> I am running RHL 7.3.
> I am trying to create a shell script with /bin/sh that will
> count how many httpd processes are running at the time.
> This is how it would look as a perl script:
> ---start of script---
> #!/usr/bin/perl
> $count = 0;
> @(protected) = (`ps -afe | grep httpd | grep -v grep`);
> foreach $entry (@(protected)) {
> $count++;
> }
> print "$count\n ";
> ---end of script---
>
> I am trying to do this in an sh script. Partly for learning
> partly because I want to do some other things to, but can
> only know how to do them in a shell script. Any help would
> be greatly appreciated.
>
> Thanks
> Steve
>
> --
> redhat-list mailing list
> unsubscribe mailto:redhat-list-request@(protected)?subject=unsubscribe
> https://www.redhat.com/mailman/listinfo/redhat-list
>
The following shell script should do the trick:
--- start of script ---
#!/bin/bash
COUNT=`ps -aef | grep httpd | grep -c -v grep`
echo $COUNT
--- end of script ---
If the reason for your question is to understand how to use a for loop, the
following script will also work:
--- start of script ---
#!/bin/bash
PROCIDLIST=`ps -aef | grep httpd | grep -v grep | awk '{print $2} '`
for PROCID in $PROCIDLIST
do
COUNT=$((COUNT+1))
done
echo $COUNT
--- end of script
Please note there is another syntax for the 'for ' command, which looks like
the following:
----
for (( statement1; statement2; statement3))
do
statement block
done
----
The above 'for ' syntax works like the 'for ' statement in C.
Michael
--
redhat-list mailing list
unsubscribe mailto:redhat-list-request@(protected)?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list
|
|
 |