How to count the number of processes in Linux

  • Misc
How to count the number of processes in Linux 1

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 like the user who created the 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. 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 process for a particular user, you need to add an entry in /etc/security/limits.conf.

user1 - nproc 5000
Don't miss out!
Subscribe To Newsletter

Want to be better at coding and CS ?

Programming tutorials

Career advice and tips

......

It's Free!

Invalid email address
Last Updated on

Leave a Reply

Your email address will not be published. Required fields are marked *