Keep watch on the number of instances of a process

Posted September 24, 2003 by Quinn McHenry in UNIX

During troubleshooting, it may be helpful to monitor the number of process instances to see if it is stable or increasing.


The following command will display the number of occurrences of sendmail processes every 5 seconds in Bourne shell derivatives (i.e., sh, ksh, bash…):

while true
do
ps -ef | grep [s]endmail | wc -l
sleep 5
done

Using a loop from the command line is a powerful tool. When you press ENTER at the end of each line, you will be presented with a different prompt based on your shell indicating that it is continuing the command and waiting for more.

The [s] in sendmail represents a regular expression trick that means match any single character within the square brackets. This means that [s]endmail will match only sendmail and will prevent counting the grep command in the ps -ef listing because it will show up as [s]endmail.

You can vary the parameter to sleep to increase or decrease the number of seconds between each counting.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.