how do I get the process list in Python?
Asked Answered
D

5

22

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.

Darrelldarrelle answered 7/7, 2009 at 9:41 Comment(2)
Are you filtering for anything specific in the process name? or just filtering for the process ids that match the process name?Av
I need to match up some streaming session in Darwin Streaming Server that doesn't have any current listeners, with the process providing the stream. Some one mentioned pgrep/pkill which also would be useful, but I think I'll use krawyoti and do os.kill from python, I'm just more comfortable writing python code then using shell commands.Darrelldarrelle
R
3

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.

Regret answered 7/7, 2009 at 10:1 Comment(3)
pgrep/pkill looks like a good solution for what I need (at least this time). I've always missed a built in ps function in Python, so that's part of the reason I posted this question. CheersDarrelldarrelle
os.popen is deprecated. Use the subprocess module.Jacklight
Huh, this is interesting. It's worth a question on its own: stackoverflow.com/questions/1098257Regret
S
36

The right portable solution in Python is using psutil. You have different APIs to interact with PIDs:

>>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, ..., 32498]
>>> psutil.pid_exists(32498)
True
>>> p = psutil.Process(32498)
>>> p.name()
'python'
>>> p.cmdline()
['python', 'script.py']
>>> p.terminate()
>>> p.wait()

...and if you want to "search and kill":

for p in psutil.process_iter():
    if 'nginx' in p.name() or 'nginx' in ' '.join(p.cmdline()):
        p.terminate()
        p.wait()
Suppression answered 28/3, 2017 at 9:39 Comment(0)
S
12

On Linux, with a suitably recent Python which includes the subprocess module:

from subprocess import Popen, PIPE

process = Popen(['ps', '-eo' ,'pid,args'], stdout=PIPE, stderr=PIPE)
stdout, notused = process.communicate()
for line in stdout.splitlines():
    pid, cmdline = line.split(' ', 1)
    #Do whatever filtering and processing is needed

You may need to tweak the ps command slightly depending on your exact needs.

Secondclass answered 7/7, 2009 at 10:2 Comment(2)
What does "suitably recent Python" mean ? 2.7 ? 3.0 ? 3.8 ?Krissykrista
@Itération122442 it means recent enough to contain the subprocess module - so 2.4-2.7, 3.5+.Secondclass
R
3

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.

Regret answered 7/7, 2009 at 10:1 Comment(3)
pgrep/pkill looks like a good solution for what I need (at least this time). I've always missed a built in ps function in Python, so that's part of the reason I posted this question. CheersDarrelldarrelle
os.popen is deprecated. Use the subprocess module.Jacklight
Huh, this is interesting. It's worth a question on its own: stackoverflow.com/questions/1098257Regret
K
0

Install psutil:

$pip install psutil

Import psutil:

>>> import psutil

Define list where process list is to be saved:

>>> processlist=list()

Append processes in list:

>>> for process in psutil.process_iter():
        processlist.append(process.name())

Get Process list:

>>> print(processlist)

Full code:

import psutil
processlist=list()
for process in psutil.process_iter():
    processlist.append(process.name())
print(processlist)
Kantar answered 18/10, 2021 at 16:47 Comment(0)
A
-11

Why Python?
You can directly use killall on the process name.

Av answered 7/7, 2009 at 9:45 Comment(4)
he said he would filter and then killBerkshire
Good observation, but why filter the ps output for the command name, gather process id values and then kill, when the killall does it for you?Av
because I need to match up the processes with other data.Darrelldarrelle
I am retaining this answer because it highlights the need for a ithin-python solution. The question has no need for the kill part; it can be simply, how do I get the process list in Python?Av

© 2022 - 2024 — McMap. All rights reserved.