简体   繁体   English

获取进程的PID号,然后使用Python将其杀死

[英]Get a PID number of a process and than kill it with Python

I use Python code that starts omxplayer. 我使用启动omxplayer的Python代码。 It plays songs and sometimes plays an advertisement during the song. 它会播放歌曲,有时还会在歌曲播放过程中播放广告。 In this case two players run. 在这种情况下,有两名选手奔跑。 The problem comes when the omxplayer times out. 当omxplayer超时时,问题就来了。 After time out I cannot close it, so till now I've used 超时后我无法关闭它,所以直到现在我已经习惯了

os.system("killall -9 omxplayer.bin")

It is ok when just one song is being played , but when an advertisement has ended during a song and the omxplayer times out (do not know why, but time out happens very often) this command kills both omxplayers. 仅播放一首歌曲就可以,但是在一首歌曲中结束广告并且omxplayer超时(不知道为什么,但是超时经常发生)时,此命令将杀死这两个omxplayer。 So it would be great if I can get the PID of the processes (omxplayer.bin) store it in a variable in python and use it to kill the process. 因此,如果我可以获取进程的PID(omxplayer.bin)并将其存储在python中的变量中并使用它杀死进程,那将是很好的。 But I would appreciate any solution which can return the PID of a given process and than kill it from the python code. 但是我很感激任何可以返回给定进程的PID并从python代码中杀死它的解决方案。 I tried so many things so far. 到目前为止,我尝试了很多事情。 Anybody can help me with this? 有人可以帮助我吗?

I'd recommend using psutil , specifically psutil.process_iter() : 我建议使用psutil ,特别是psutil.process_iter()

>>> # get all PIDs
>>> psutil.pids()
[0, 4, 472, 648, 756, ...]
>>> # but for iteration you should use process_iter()
>>> for proc in psutil.process_iter():
>>>     try:
>>>         if pinfo.name() == "omxplayer":
>>>             pinfo.terminate()
>>>     except psutil.NoSuchProcess:
>>>         pass

A possible solution, as I commented: 正如我评论的那样,一个可能的解决方案:

import os
import signal
from subprocess import check_output

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

def main():
    os.kill(get_pid(whatever_you_search), signal.SIGTERM) #or signal.SIGKILL 

if __name__ == "__main__":
    main()

I know this question is old, but here is what I came out with, it works for java proceses: 我知道这个问题很旧,但是这是我想出来的,它适用于Java过程:

First get the pid 首先获取pid

def getPidByName(name):
    process = check_output(["ps", "-fea"]).split('\n')
    for x in range(0, len(process)):
        args = process[x].split(' ')
        for j in range(0, len(args)):
            part = args[j]
            if(name in part):
                return args[2]

And then kill it pid = getPidByName("user-access") os.kill(int(pid), signal.SIGKILL) 然后杀死它pid = getPidByName(“ user-access”)os.kill(int(pid),signal.SIGKILL)

just as @ElTête mentioned. 就像@ElTête提到的那样。

For different process you might have to adjust the return index in args, depending in the process you are trying to kill. 对于不同的进程,您可能必须根据要杀死的进程来调整args中的返回索引。 Or just iterate across the command and kill where you find an occurrence of the name. 或者只是遍历命令并杀死在其中找到该名称的位置。 This mainly because "pidof" won't work on macOs or windows. 这主要是因为“ pidof”在macO或Windows上不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM