按名称杀死进程?
我正在尝试终止一个进程(特别是 iChat)。在命令行上,我使用以下命令:
ps -A | grep iChat
然后:
kill -9 PID
但是,我不太确定如何将这些命令转换为 Python。
I'm trying to kill a process (specifically iChat). On the command line, I use these commands:
ps -A | grep iChat
Then:
kill -9 PID
However, I'm not exactly sure how to translate these commands over to Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
psutil 可以通过名称找到进程并杀死它:
psutil can find process by name and kill it:
假设您使用的是类 Unix 平台(因此存在
ps -A
),则在
out
ps -A 的输出> 变量(字符串)。您可以将其分解为几行并对其进行循环...:(您可以避免导入
signal
,并使用9
而不是signal.SIGKILL
>,但我只是不太喜欢这种风格,所以我宁愿以这种方式使用命名常量)。当然,您可以对这些线路进行更复杂的处理,但这模仿了您在 shell 中所做的事情。
如果您想要避免使用
ps
,那么在不同的类 Unix 系统中很难做到这一点(从某种意义上来说,ps
是它们获取进程列表的通用 API) 。但是,如果您只考虑一个特定的类 Unix 系统(不需要任何跨平台可移植性),那么这是可能的;特别是,在 Linux 上,/proc
伪文件系统非常有用。但您需要先明确您的具体要求,然后我们才能为后一部分提供帮助。Assuming you're on a Unix-like platform (so that
ps -A
exists),gives you
ps -A
's output in theout
variable (a string). You can break it down into lines and loop on them...:(you could avoid importing
signal
, and use9
instead ofsignal.SIGKILL
, but I just don't particularly like that style, so I'd rather used the named constant this way).Of course you could do much more sophisticated processing on these lines, but this mimics what you're doing in shell.
If what you're after is avoiding
ps
, that's hard to do across different Unix-like systems (ps
is their common API to get a process list, in a sense). But if you have a specific Unix-like system in mind, only (not requiring any cross-platform portability), it may be possible; in particular, on Linux, the/proc
pseudo-filesystem is very helpful. But you'll need to clarify your exact requirements before we can help on this latter part.如果您必须考虑 Windows 情况才能跨平台,请尝试以下操作:
If you have to consider the Windows case in order to be cross-platform, then try the following:
如果你有killall:
或者:
If you have killall:
Or:
你可以试试这个。
但在您需要使用
sudo pip install psutil
安装 psutil 之前You can try this.
but before you need to install psutil using
sudo pip install psutil
这在 Windows 7 中对我有用
this worked for me in windows 7
下面的代码将杀死所有面向 iChat 的程序:
The below code will kill all iChat oriented programs:
使用
Process
获取流程对象。Get the process object using the
Process
.您可以使用
psutil
模块使用其名称来终止进程。在大多数情况下,这应该是跨平台的。我基本上扩展了@Giampaolo Rodolà的答案。
我还将此代码段发布为 gist。
注意:一旦您对观察到的所需行为感到满意,就可以删除打印语句。
You can use the
psutil
module to kill a process using it's name. For the most part, this should be cross platform.I have basically extended @Giampaolo Rodolà's answer.
I have also posted this snippet as a gist.
Note: You can remove the print statements once you are satisfied that the desired behavior is observed.
你可以使用 WMI 模块在 Windows 上执行此操作,尽管它比你的 Unix 用户习惯的要笨重得多;
导入 WMI
需要很长时间,而且整个过程会很痛苦。you can use WMI module to do this on windows, though it's a lot clunkier than you unix folks are used to;
import WMI
takes a long time and there's intermediate pain to get at the process.如果您想终止带有特定标题的进程或 cmd.exe。
希望有帮助。他们可能有很多比我更好的解决方案。
If you want to kill the process(es) or cmd.exe carrying a particular title(s).
Hope it helps. Their might be numerous better solutions to mine.
Alex Martelli 的答案在 Python 3 中不起作用,因为
out
将是一个字节对象,从而导致TypeError: a bytes-like object is required, not 'str'
测试if 'iChat' in line:
时。引用 subprocess 文档:
对于 Python 3,可以通过将
text=True
(>= Python 3.7) 或universal_newlines=True
参数添加到Popen
构造函数来解决。然后out
将作为字符串对象返回。或者,您可以使用字节的decode() 方法创建一个字符串。
The Alex Martelli answer won't work in Python 3 because
out
will be a bytes object and thus result in aTypeError: a bytes-like object is required, not 'str'
when testingif 'iChat' in line:
.Quoting from subprocess documentation:
For Python 3, this is solved by adding the
text=True
(>= Python 3.7) oruniversal_newlines=True
argument to thePopen
constructor.out
will then be returned as a string object.Alternatively, you can create a string using the decode() method of bytes.
与 Giampaolo Rodolà 的回答风格相同,但作为一个衬里,不区分大小写,并且不必匹配整个进程名称,在 Windows 中,您必须包含
.exe
后缀。In the same style as Giampaolo Rodolà' answer but as one liner, case insensitive and without having to match the whole process name, in windows you would have to include the
.exe
suffix.对我来说唯一有效的是:
例如
For me the only thing that worked is been:
For example
您可以在 unix 系统中使用
pkill
按名称终止进程。那么Python代码将是:
You can use
pkill <process_name>
in a unix system to kill process by name.Then the python code will be: