[英]How to kill process by name in python
>>> os.system('adb shell pidof logcat')
750 4774
0
>>> os.system('adb shell ps | grep logcat')
root 750 1 8760 1720 __skb_recv 7f8f5a5edc S /system/bin/logcat
root 4774 4681 8708 1696 __skb_recv 7f98efeedc S logcat
There are 2 process of logcat. logcat有2个进程。 How to kill both process Id : 750 4774如何杀死两个进程 ID:750 4774
import subprocess
subprocess.call(['taskkill.exe', '/IM', 'logcat'])
You might need to use shell=True
if it can't find taskkill.exe.如果找不到 taskkill.exe,您可能需要使用shell=True
。
Introducing what I like to call kill.py
介绍我喜欢称之为kill.py
import psutil
command_substring_to_purge = "app.py"
pids_to_kill = []
processes = psutil.process_iter(attrs=["pid", "cmdline"])
for process in processes:
pid = process.info["pid"]
for subcommands in process.info["cmdline"]:
if command_substring_to_purge in subcommands:
pids_to_kill.append(pid)
break
for pid in pids_to_kill:
process = psutil.Process(pid)
process.terminate()
Note that to kill root programs you will need to run this as a root.请注意,要杀死 root 程序,您需要以 root 身份运行它。
With great power comes great responsibility.拥有权利的同时也被赋予了重大的责任。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.