Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process list on Linux via Python

Tags:

python

linux

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

like image 314
Madzombie Avatar asked Apr 24 '10 07:04

Madzombie


People also ask

How do I see all Python processes in Linux?

I usually use ps -fA | grep python to see what processes are running. The CMD will show you what python scripts you have running, although it won't give you the directory of the script.

How do I list running processes in Python?

We would be using the WMI. Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI. Win32_Process() to get the running processes, iterated through each process and stored in variable process.


1 Answers

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 
like image 144
bobince Avatar answered Sep 23 '22 01:09

bobince