如何通过进程名获取PID?

58

在Python中,我是否有办法通过进程名称获取PID?

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                        
 3110 meysam    20   0  971m 286m  63m S  14.0  7.9  14:24.50 chrome 

比如说我需要通过 chrome 获取 3110


1
可能会有多个具有相同名称但不同PID的进程。 - Adam Jenča
10个回答

77

您可以使用 pidofsubprocess.check_output 按名称获取进程的 pid:

from subprocess import check_output
def get_pid(name):
    return check_output(["pidof",name])


In [5]: get_pid("java")
Out[5]: '23366\n'

check_output(["pidof",name]) 将会运行命令 "pidof 进程名称"如果返回值非零则会引发 CalledProcessError。

要处理多个条目并将它们转换为整数:

from subprocess import check_output
def get_pid(name):
    return map(int,check_output(["pidof",name]).split())

在 [21] 中:获取进程 ID(PID)的函数 get_pid("chrome"),其中参数为 "chrome"。

Out[21]: 
[27698, 27678, 27665, 27649, 27540, 27530, 27517, 14884, 14719, 13849, 13708, 7713, 7310, 7291, 7217, 7208, 7204, 7189, 7180, 7175, 7166, 7151, 7138, 7127, 7117, 7114, 7107, 7095, 7091, 7087, 7083, 7073, 7065, 7056, 7048, 7028, 7011, 6997]

或者传递-s标志获取单个pid:

def get_pid(name):
    return int(check_output(["pidof","-s",name]))

In [25]: get_pid("chrome")
Out[25]: 27698

3
与其调用外部工具,为什么不直接通过/proc条目进行操作呢?在bash脚本中很常见,但在Python脚本中这样做通常不是很规范。此外,如果有多个具有相同名称的进程怎么办?至少我会使用splitlines()将输出拆分成行,并将PID转换为整数。 - ThiefMaster
1
@ThiefMaster,使用pidof命令不够干净的地方在哪里? - Padraic Cunningham
请注意,check_output() 在Python 2.6及更早版本中不可用。 - Jaime M.
1
@JaimeM,实现https://gist.github.com/edufelipe/1027906非常简单。 - Padraic Cunningham
7
由于这个答案的发布者没有活跃,所以我来找你。当我使用第一段代码时,它会抛出一个“FileNotFoundError”,为什么会这样? - Voldemort's Wrath
显示剩余6条评论

19

您可以使用psutil软件包:

安装


pip install psutil

使用方法:

import psutil

process_name = "chrome"
pid = None

for proc in psutil.process_iter():
    if process_name in proc.name():
       pid = proc.pid
       break

print("Pid:", pid)

非常感谢,能够完美地与Python 3配合使用! - Andrew T.

12

您也可以使用pgrep,在pgrep中,您还可以提供匹配模式进行匹配

import subprocess
child = subprocess.Popen(['pgrep','program_name'], stdout=subprocess.PIPE, shell=True)
result = child.communicate()[0]

您也可以像这样使用 awk 命令与 ps 命令配合使用

ps aux | awk '/name/{print $2}'

你可以使用 subprocess 模块中的一个辅助函数,这样代码会更易读。另外,在 shell 中执行它是一个非常糟糕的想法! - ThiefMaster
是的,这只是一个概念,我已经提供给了 OP。 - Hackaholic
1
最好使用 shell=False 运行。 - sobolevn
太棒了!我已经根据您的答案编写了一个完整的示例,链接在这里:https://dev59.com/zF8d5IYBdhLWcg3wkSwV#44712205 - Dennis Golomazov

11

对于posix(仅需要挂载/proc目录的Linux、BSD等),在/proc中处理操作系统文件更容易。

这是纯Python,无需调用外部shell程序。

适用于Python 2和3(唯一的区别(2to3)是异常树,因此我不喜欢“except Exception”,但为了保持兼容性而保留。也可以创建自定义异常。)

#!/usr/bin/env python

import os
import sys


for dirname in os.listdir('/proc'):
    if dirname == 'curproc':
        continue

    try:
        with open('/proc/{}/cmdline'.format(dirname), mode='rb') as fd:
            content = fd.read().decode().split('\x00')
    except Exception:
        continue

    for i in sys.argv[1:]:
        if i in content[0]:
            print('{0:<12} : {1}'.format(dirname, ' '.join(content)))

示例输出(其工作方式类似于pgrep):

phoemur ~/python $ ./pgrep.py bash
1487         : -bash 
1779         : /bin/bash

在Solaris家族中,/proc是二进制的,而不是文本的。 - user933161

9

这是基于@Hackaholic的答案的完整示例:

def get_process_id(name):
    """Return process ids found by (partial) name or regex.

    >>> get_process_id('kthreadd')
    [2]
    >>> get_process_id('watchdog')
    [10, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61]  # ymmv
    >>> get_process_id('non-existent process')
    []
    """
    child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False)
    response = child.communicate()[0]
    return [int(pid) for pid in response.split()]

6
为了进一步改善Padraic的回答:当check_output返回非零代码时,它会引发一个CalledProcessError。这是因为进程不存在或未运行。

我会做的是捕获这个异常:

#!/usr/bin/python

from subprocess import check_output, CalledProcessError

def getPIDs(process):
    try:
        pidlist = map(int, check_output(["pidof", process]).split())
    except  CalledProcessError:
        pidlist = []
    print 'list of PIDs = ' + ', '.join(str(e) for e in pidlist)

if __name__ == '__main__':
    getPIDs("chrome")

输出结果:
$ python pidproc.py
list of PIDS = 31840, 31841, 41942

在这种情况下,由于我们在try/catch中没有执行任何有用的操作,因此我们也可以使用subprocess.call而不是带有try/catch块的subprocess.check_output - Rajan Ponnappan
@RajanPonnappan 使用 subprocess.call 只能获取返回代码 ($?),然而 subprocess.check_output 返回你真正想要的:命令输出(在这种情况下是 PID 列表)。 - Alejandro Blasco
1
你是对的。我混淆了check_callcheck_output的行为。 - Rajan Ponnappan

3
如果您正在使用Windows操作系统,可以使用以下代码获取进程/应用程序的PID与其映像名称:
from subprocess import Popen, PIPE

def get_pid_of_app(app_image_name):
    final_list = []
    command = Popen(['tasklist', '/FI', f'IMAGENAME eq {app_image_name}', '/fo', 'CSV'], stdout=PIPE, shell=False)
    msg = command.communicate()
    output = str(msg[0])
    if 'INFO' not in output:
        output_list = output.split(app_image_name)
        for i in range(1, len(output_list)):
            j = int(output_list[i].replace("\"", '')[1:].split(',')[0])
            if j not in final_list:
                final_list.append(j)

    return final_list

它将返回一个应用程序(如Firefox或Chrome)的所有PID,例如。

>>> get_pid_of_app("firefox.exe")
[10908, 4324, 1272, 6936, 1412, 2824, 6388, 1884]

如果有帮助的话,请告诉我。


2

If your OS is Unix base use this code:

import os
def check_process(name):
    output = []
    cmd = "ps -aef | grep -i '%s' | grep -v 'grep' | awk '{ print $2 }' > /tmp/out"
    os.system(cmd % name)
    with open('/tmp/out', 'r') as f:
        line = f.readline()
        while line:
            output.append(line.strip())
            line = f.readline()
            if line.strip():
                output.append(line.strip())

    return output

然后调用它并传递一个进程名称以获取所有PID。将其称为“最初的回答”。
>>> check_process('firefox')
['499', '621', '623', '630', '11733']

1
自Python 3.5起,建议使用subprocess.run()而不是subprocess.check_output()
>>> int(subprocess.run(["pidof", "-s", "your_process"], stdout=subprocess.PIPE).stdout)

自 Python 3.7 起,您可以使用 capture_output=true 参数捕获 stdout 和 stderr:
>>> int(subprocess.run(["pidof", "-s", "your process"], capture_output=True).stdout)

0
在Unix系统中,您可以使用pyproc2包。 安装。
pip install pyproc2
用法
import pyproc2
chrome_pid=pyproc2.find("chrome").pid #Returns PID of first process with name "chrome"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接