Take the 2-minute tour ×

Several processes with the same name are running on host. What is the cross-platform way to get PIDs of that processes by name using python or jython?

  1. It's like pidof but in python (I don't have pidof anyway).
  2. I can't parse /proc because it might be unavailable (on HP-UX).
  3. 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).
  4. Target platforms are Solaris, HP-UX, and maybe others.
share|improve this question

7 Answers 7

up vote 33 down vote accepted

You can use psutil (http://code.google.com/p/psutil) which works on Windows and UNIX:

import psutil

PROCNAME = "python.exe"

for proc in psutil.process_iter():
    if proc.name == PROCNAME:
        print proc

The code above on my machines prints:

psutil.Process (PID:2908; PPID:3944; NAME:'python.exe'; PATH:'C:\Python26'; CMDL
INE:['C:\\Python26\\python.exe', 'D:\\pyftpdlib\\svn\\psutil\\foo.py']; UID:-1;
share|improve this answer
You´ve made very useful module. Thank you. –  Alex Bolotov Feb 11 '10 at 0:16
Few clicks and I'm done (even in Windows). Great. –  Zippoxer Mar 28 '11 at 17:07
Unfortunately OS X does not allow you to access many attributes of a process (name, exe, cmdline), even if you only try to access those processes that are created by you. Unless you run the interpreter/script with sudo, that is. –  John Sep 20 '11 at 23:40
Yes, that's a limitation of OSX (and it's the only platform behaving like that). There's nothing you can do about it except using sudo/setuid. –  Giampaolo Rodolà Feb 3 '12 at 9:17
Update - this is now fixed for different methods, see: code.google.com/p/psutil/issues/detail?id=297 –  Giampaolo Rodolà Mar 7 '13 at 0:25

There's no single cross-platform API, you'll have to check for OS. For posix based use /proc. For Windows use following code to get list of all pids with coresponding process names

from win32com.client import GetObject
WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')
process_list = [(p.Properties_("ProcessID").Value, p.Properties_("Name").Value) for p in processes]

You can then easily filter out processes you need. For more info on available properties of Win32_Process check out Win32_Process Class

share|improve this answer
This library doesn't come standard with Python, at least not with 2.7. Didn't check the other versions. –  Zoran Pavlovic Oct 11 '12 at 9:25
import psutil

process = filter(lambda p: p.name == "YourProcess.exe", psutil.process_iter())
for i in process:
  print i.name,i.pid

Give all pids of "YourProcess.exe"

share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

For jython, if Java 5 is used, then you can get the Java process id as following:

from java.lang.management import *
pid = ManagementFactory.getRuntimeMXBean().getName()

share|improve this answer
that works, but I want to get PID of another process (like pidof do but without using pidof) –  Alex Bolotov Apr 15 '09 at 20:46

Your Answer


