Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using python or jython?
- I want something like
pidofbut in python. (I don’t havepidofanyway.) - I can’t parse
/procbecause it might be unavailable (on HP-UX). - I do not want to run
os.popen('ps')and parse the output because I think it is ugly (field sequence may be different in different OS). - Target platforms are Solaris, HP-UX, and maybe others.
You can use psutil (https://github.com/giampaolo/psutil), which works on Windows and UNIX:
On my machine it prints:
EDIT 2017-04-27 – here’s a more advanced utility function which checks the name against processes’ name(), cmdline() and exe():