How to get (from terminal) total number of threads (per process and total for all processes)

To get the number of threads for a given pid:

ps -o nlwp <pid>

To the get the sum of all threads running in the system:

ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'

For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.


I'm basing this answer around ps axms. ps is a great tool for listing what's running.

If you want to filter that by a process, you could try something like this:

echo $(( `ps axms | grep firefox | wc -l`  - 1))

We subtract 1 because grep will show in that list.

For all threads in general this should work:

echo $(( `ps axms | wc -l`  - 1))

We subtract one this time because there is a header row.

Tags:

System Info