Cross-platform way to get PIDs by process name in python

后端 未结 8 1068
后悔当初
后悔当初 2020-12-02 09:35

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 jyth

相关标签:
8条回答
  • 2020-12-02 10:11

    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.

    0 讨论(0)
  • 2020-12-02 10:14

    A note on ThorSummoner's comment

    process = [proc for proc in psutil.process_iter() if proc.name == "YourProcess.exe"].
    

    I have tried it on Debian with Python 3, I think it has to be proc.name() instead of proc.name.

    0 讨论(0)
  • 2020-12-02 10:20

    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)

    0 讨论(0)
  • 2020-12-02 10:23

    You can use psutil (https://github.com/giampaolo/psutil), which works on Windows and UNIX:

    import psutil
    
    PROCNAME = "python.exe"
    
    for proc in psutil.process_iter():
        if proc.name() == PROCNAME:
            print(proc)
    

    On my machine it prints:

    <psutil.Process(pid=3881, name='python.exe') at 140192133873040>
    

    EDIT 2017-04-27 - here's a more advanced utility function which checks the name against processes' name(), cmdline() and exe():

    import os
    import psutil
    
    def find_procs_by_name(name):
        "Return a list of processes matching 'name'."
        assert name, name
        ls = []
        for p in psutil.process_iter():
            name_, exe, cmdline = "", "", []
            try:
                name_ = p.name()
                cmdline = p.cmdline()
                exe = p.exe()
            except (psutil.AccessDenied, psutil.ZombieProcess):
                pass
            except psutil.NoSuchProcess:
                continue
            if name == name_ or cmdline[0] == name or os.path.basename(exe) == name:
                ls.append(p)
        return ls
    
    0 讨论(0)
  • 2020-12-02 10:25

    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()

    0 讨论(0)
  • 2020-12-02 10:30

    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

    0 讨论(0)
提交回复
热议问题