how do I get the process list in Python?

后端 未结 4 1200
感情败类
感情败类 2020-12-06 01:51

How do I get a process list of all running processes from Python, on Unix, containing then name of the command/process and process id, so I can filter and kill processes.

4条回答
  •  猫巷女王i
    2020-12-06 02:36

    On linux, the easiest solution is probably to use the external ps command:

    >>> import os
    >>> data = [(int(p), c) for p, c in [x.rstrip('\n').split(' ', 1) \
    ...        for x in os.popen('ps h -eo pid:1,command')]]
    

    On other systems you might have to change the options to ps.

    Still, you might want to run man on pgrep and pkill.

提交回复
热议问题