How do I count how many processes are running in Linux?

ADVERTISEMENTS



Viewing running processes in Linux

How do I see how many processes are running in Linux? How can count all running processes in Linux?A Linux process is nothing but running instance of a program or command. This page explains various commands to count the number of processes running in Linux.

The ps command used to list the currently running processes and their PIDs in Linux and Unix-like systems. At a bare minimum, two processes displayed on the screen. For example, bash and ps might default on Linux when you just type ps command ps

Sample outputs:

PID TTY TIME CMD 31043 pts/3 00:00:00 bash 32324 pts/3 00:00:00 ps

Counts for each file in Linux

The wc is an acronym for word count. By default, wc command counts the number of lines, words, and characters in the text. For examples, show the newline counts

echo "line 1" | wc -l

To print the byte counts

echo "Hello" | wc -c

One can print the word counts as follows:

echo "Hello world" | wc -w



Find how many processes are running in Linux

One can use the ps command along with with the wc command to count the number of processes running on your Linux based system by any user. It is best to run the following commands as root user using the sudo command.

Command to count the number of processes running in Linux

The Linux syntax is as follows:

# ps -e | wc -l

To see and count every process on the system using BSD syntax:

# ps axu | wc -l

Want to see and count every process running as vivek (real and effective ID) in user format, run:

$ ps -U vivek -u vivek u | wc -l

Another example for www-data user:

$ ps -U www-data -u www-data u | wc -l

In short to see and count only processes by a certain user naned root, you can use the following command:

sudo ps -U root | wc -l

sudo ps -U root -u root u | wc -l

Next we are going count process IDs of nginx using the following syntax:

ps -C nginx | wc -l

ps -C nginx -o pid= | wc -l



Pass the --no-headers or --no-heading to print no header line at all to get processes count correctly on Linux:

# ps -e --no-headers | wc -l

52

# ps -e | wc -l

53

When count real number of nginx it is a good idea to remove grep command while grepping using ps command:

ps -e --no-headers | grep [n]ginx

ps -e --no-headers | grep [n]ginx | wc -l

Understanding the wc command options

wc option description -c Print the byte counts -m Print the character counts -l Print the newline counts -w print the word counts --help Display the wc command help and exit

Understanding the ps command options

ps option description -e Select all processes (GNU/Linux syntax) aux Select all processes using BSD syntax -U user Select by real user ID (RUID) or name -u user Select by effective user ID (EUID) or name -C cmdlist Select by command name. This selects the processes whose executable name is given in cmdlist --no-headers Print no header line at all. --no-heading is an alias for this option

Conclusion

You learned how to list the number of processes running on the Linux or Unix like system using various command-line options. See the gnu ps help page here.