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
pidof
but in python. (I don't havepidof
anyway.) - I can't parse
/proc
because 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.
Give all pids of "YourProcess.exe"
A note on ThorSummoner's comment
I have tried it on Debian with Python 3, I think it has to be
proc.name()
instead ofproc.name
.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():
First, Windows (in all it's incarnations) is a non-standard OS.
Linux (and most proprietary unixen) are POSIX-compliant standard operating systems.
The C libraries reflect this dichotomy. Python reflects the C libraries.
There is no "cross-platform" way to do this. You have to hack up something with ctypes for a particular release of Windows (XP or Vista)
I don't think you will be able to find a purely python-based, portable solution without using /proc or command line utilities, at least not in python itself. Parsing os.system is not ugly - someone has to deal with the multiple platforms, be it you or someone else. Implementing it for the OS you are interested in should be fairly easy, honestly.
There isn't, I'm afraid. Processes are uniquely identified by pid not by name. If you really must find a pid by name, then you will have use something like you have suggested, but it won't be portable and probably will not work in all cases.
If you only have to find the pids for a certain application and you have control over this application, then I'd suggest changing this app to store its pid in files in some location where your script can find it.