I have the following bash script, that lists the current number of httpd processes, and if it is over 60, it should email me. This works 80% of the time but for some reason sometimes it emails me anyways when it is not over 60. Any ideas?
#!/bin/bash
lines=`ps -ef|grep httpd| wc -l`
if [ "$lines" -gt "60" ]
then
mailx -s "Over 60 httpd processes" me@me.com < /dev/null
fi
httpdprocesses might finish, or start, or both. So, the number of processes can be different.grepprocess in the processes (most of the time, it could happen that thepsfinishes beforegrepstarts). An easy way to avoid that is to change your command tops -ef | grep [h]ttpd. This will make sure thatgrepdoesn’t matchgrep [h]ttpd.pgrep, which might be better suited for your purposes.grep ... | wc -lcan usually be replaced withgrep -c ....