How can I get running process list using Python on Linux?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The sanctioned way of creating and using child processes is through the subprocess module.
The command is broken down into a python list of arguments so that it does not need to be run in a shell (By default the subprocess.Popen does not use any kind of a shell environment it just execs it). Because of this we cant simply supply 'ps -U 0' to Popen.
I would use the subprocess module to execute the command
ps
with appropriate options. By adding options you can modify which processes you see. Lot's of examples on subprocess on SO. This question answers how to parse the output ofps
for example:)You can, as one of the example answers showed also use the PSI module to access system information (such as the process table in this example).
You can use a third party library, such as PSI:
You could use psutil as a platform independent solution!
IMO looking at the
/proc
filesystem is less nasty than hacking the text output ofps
.