How many processes where created during one minute?

Viewed 191

I would like to know how many processes the Linux kernel created during a period of time.

Usually during one minute.

My background: If too many processes got created during a minute, then there is something wrong. Most of our legacy code base was moved from shell to python, but sometimes there are still some shell scripts which are slow because they a lot of processes.

I would like to create a graph from this number. Then I would like to check on which host and why so many processes got created.

I want to implement this with Python.

Answers how to read this from /proc or /sys would be great.

It would be nice if the solution works for the wrap around which happens if pid_max gets reached.

The limit (maximum number of pids) is /proc/sys/kernel/pid_max. The manual says:

/proc/sys/kernel/pid_max (since Linux 2.5.34)

This file specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). The default value for this file, 32768, results in the same range of PIDs as on earlier kernels

3 Answers

check /proc/stat, there is a processes field, counts numbers of fork since boot, doc:

$ grep processes /proc/stat
processes 81579558

I use this under windows but maybe you can try it as a starting point

>>> import subprocess
>>> subprocess.Popen('tasklist')
<subprocess.Popen object at 0x00000268164C3CC0>
>>>
Name                           PID     Session name   No. of s  Utilisation
========================= ======== ================ =========== ============

this will give you a table which you can capture with

subprocess.Popen('tasklist').communicate()[0], just count the lines and you'll get the current number of processes. Do it again in 1 minute and see what's changed

Instead of manually looking at /proc or /sys , let linux do it for you:

import subprocess
from time import sleep

time = 0
ps = subprocess.Popen(["ps",'-A', '-o', 'pid'], stdout=subprocess.PIPE)
pids = [int(x) for x in ps.communicate()[0].split()[1:]]
new_pids_count = 0;

while time < 60:
    ps = subprocess.Popen(["ps",'-A', '-o', 'pid'], stdout=subprocess.PIPE)
    output = [int(x) for x in ps.communicate()[0].split()[1:]]
    for x in output:
        if x not in pids:
            new_pids_count += 1
            pids.append(x)
    time += 1
    sleep(1)

Initially, I get all the currently running PIDS, using ps -A -i -pid, and put them all in a list.
I repeat this every second, to check for the newly spawned process, by comparing the results from the running it again, and the growing pids list.

Related