Coder's Cat

How to count the number of processes in Linux

2020-01-08

Count the number of running processes

The shell command ps could be used to list most process statistics.

We can use this command piped with wc to get the number of running processes in Linux, remember to remove the header of output, --no-headers added.

-L will also display threads.

$ ps -A --no-headers | wc -l
232

$ ps -AL --no-headers | wc -l
456

If you want to count the processes of a specific user, use the following command:

$ ps -U user1 --no-headers | wc -l
36

If you want to count the number of spawned process by httpd, these two commands will be your choice:

$ ps -C httpd --no-headers | wc -l
8

$ pgrep httpd | wc -l
8

pgrep is a very useful command to lookup running processes based on name, process ID and other attributes of a process.

Get the maximum number of processes

You may be curious about the number limit of running processes in a system.

On Linux, you could check the maximum number of processes with the command:

$ cat /proc/sys/kernel/pid_max
32768

Or with sysctl command:

$ sysctl kernel.pid_max
32768

Usually, 4194303 is the maximum limit for x86_64 and 32767 for x86. If you want to change the maximum number, you could write to /etc/sysctl.conf:

kernel.pid_max = 4194303

Process number limit per user

The root user doesn’t have a limit on the number of processes, ulimit -a will show all kinds of limitation of resource for a user. file:img/2020_01_08_count-processes-on-linux.org_20200108_185822.png

Normally, a non-root user has a limit on the number of processes. If you want to change the limit on the number of processes for a particular user, you need to add an entry in /etc/security/limits.conf.

user1 - nproc 5000

Join my Email List for more insights, It's Free!😋

Tags: Tools