Mailing List
Home
Linux - General Red Hat Linux discussion list
Enterprise Linux 3 - Discussion of Red Hat Enterprise Linux 3 (Taroon)
Installation - Getting started with Red Hat Linux
Red Hat Linux 9 - Discussion of Red Hat Linux 9 (Shrike)
Red Hat Linux 7.3 - Discussion of Red Hat Linux 7.3 (Valhalla)
Red Hat Linux 7.2 - Discussion of Red Hat Linux 7.2 (Enigma)
Apache Web Server
Oracle database, Microsoft SQL server ...
Subjects
application/x mplayer2 plugin
RPM error: db4 error(16) from dbenv >remove: Device or resource
   busy
Command stream end of file while reading
X Windows problem (xauth)
Upgrading openoffice 1 1 rpm
FTP: connection refused
FTP: connection refused
mount: /dev/cdrom: is not a valid block device
Dell Precision 650, RedHat 9, no sound
how to trace the cause resulting in the crash of bind server
Virus on the list
UNINSTALL RPM MYSQL
usb pen drives: mounting as a user
broadcom network interface
make mrproper
sendmail configuration on redhat
Couldn 't open PID file /var/run/named/named pid Permission denied
Promise 378 controller
kernel 2 6 and /dev/sound/mixer not found
Problem using up2date
mrtg step by step howto/configuration for a newbie?
Compiling and Installing Kernel 2 6
Can 't locate module ppp0, can 't locate module ppp compress 21
HOW I CAN MAKE BOOTABLE FLOPPY DISKET
Lotus Notes under Wine
/etc/security/limits conf question
Intel E/1000 driver
Command stream end of file while reading
rpm database corrupt
qla2300 modules
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
shell script to count httpd processes

shell script to count httpd processes

2005-03-14       - By Michael Velez

 Back
Reply:     1     2     3     4     5     6     7     8     9     10     >>  



> -- --Original Message-- --
> From: redhat-list-bounces@(protected)
> [mailto:redhat-list-bounces@(protected)] On Behalf Of Steve Buehler
> Sent: Monday, March 14, 2005 3:35 PM
> To: General Red Hat Linux discussion list
> Subject: RE: shell script to count httpd processes
>
> At 01:43 PM 3/14/2005, you wrote:
> > > 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.
> >
> >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.
>
> Steve, Brian and Michael.
> Thank you all for your responses. I had tried the
> wc -l option and couldn 't get it to work. Figured it was
> because it was for files. It was just because I tried it in
> every way but the correct way.
> Michael
> You gave me just what I was looking for. A little
> knowledge of the for loop. Was hoping for a loop that would
> actually count the lines and not the words though. But your
> awk command helped me there. I will continue hunting for an
> answer to my "learning question " where I am just trying to
> find out how to count whole lines in a shell script variable.
> with a loop internally to the script without the wc command.
> Thanks
> Steve
>
>
> --
> redhat-list mailing list
> unsubscribe mailto:redhat-list-request@(protected)?subject=unsubscribe
> https://www.redhat.com/mailman/listinfo/redhat-list
>

Steve,

If you want to know how to read one line at a time from a variable and feed
that line to your for loop, you can use the following script:

--- start of script ---
#!/bin/bash
COUNT=0
IFS=$ '\n '

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

The IFS variable tells the shell script how to delimit words in a variable.
It is usually set to the following:
IFS=$ ' \t\n '

in other words, spaces, tabs, and newlines usually delimit words in a
variable. In the above script, I have changed it to just have newlines
delimit words.

Michael

--
redhat-list mailing list
unsubscribe mailto:redhat-list-request@(protected)?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list