Process list on Linux via Python


Question

How can I get running process list using Python on Linux?

1
84
4/24/2010 8:01:42 AM

IMO looking at the /proc filesystem is less nasty than hacking the text output of ps.

import os
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]

for pid in pids:
    try:
        print open(os.path.join('/proc', pid, 'cmdline'), 'rb').read().split('\0')
    except IOError: # proc has already terminated
        continue
120
9/10/2018 9:32:53 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon