这是我现在拥有的当前代码,我想知道如何让它杀死pid。
import commands, signal
stuid = commands.getoutput("pgrep student")
deaid = commands.getoutput("pgrep daemon")
print stuid
os.kill(stuid, signal.SIGKILL)
print deaid
os.kill(deaid, signal.SIGKILL)
编辑: 所以,最后,我只是使用os.system让终端运行kill命令,然后在kill之后放置pid。
import commands, os
stuid = commands.getoutput("pgrep student")
deaid = commands.getoutput("pgrep daemon")
print stuid
os.system("kill "+stuid)
print deaid
os.system("kill "+deaid)
总的来说,这是我的最终结果。希望这有助于未来的人们。
答案 0 :(得分:2)
阅读this answer。
BTW更多的pythonic解决方案可能是这样的: import re
import psutil
convicted = re.compile(r'student|daemon')
for p in psutil.process_iter():
if convicted.search(p.name):
p.terminate()
修改:为了更准确,我将行p.kill()
更改为p.terminate()
。 bash中的公共kill
实际上与p.terminate()
相同(它发送TERM信号)。但是p.kill()
对应于bash中的kill -9
(它发送KILL信号)。