在 linux (Python) 中使用其名称终止进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1610609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:42:21  来源:igfitidea点击:

Kill the process using its name inside linux (Python)

pythonlinuxunixprocess

提问by TIMEX

this works, but it kills every Python process.

这有效,但它会杀死每个 Python 进程。

pkill python

However, I cannot do:

但是,我不能这样做:

pkill myscript.py

I have also tried killall, but with no luck either. Do I have to user regular expressions?

我也试过killall,但也没有运气。我必须使用正则表达式吗?

By the way, I want to do this in a python script with import os.

顺便说一句,我想在一个带有import os的 python 脚本中做到这一点

采纳答案by Ryan Bright

Did you launch the Python subprocess from the same script you are killing it from? If so, see this questionfor details. If not, you can use pkill's -f option to search for the script name in the Python process's argument list, but you still run the risk of killing something you didn't intend to. See the man pagefor more info.

您是否从杀死它的同一个脚本中启动了 Python 子进程?如果是这样,请参阅此问题以了解详细信息。如果没有,您可以使用pkill's -f 选项在 Python 进程的参数列表中搜索脚本名称,但您仍然冒着杀死不想要的东西的风险。有关更多信息,请参阅手册页

回答by John La Rooy

Are you able to have the process write it's pidto a file?
In Python you get the pid like this:

您能否让进程将其写入pid文件?
在 Python 中,您可以像这样获得 pid:

import os
os.getpid()

Killing by name is convenient, but sometimes has undesired consequences as you have seen.

按名称杀戮很方便,但有时会产生不希望的后果,如您所见。

回答by prabhakaran9397

sudo kill -9 `pgrep python`

sudo kill -9 `pgrep python`

This command will kill all the running python processes

此命令将杀死所有正在运行的 python 进程

回答by jldupont

you need to lookup the process id (pid). You can use the command "ps -A | grep NAME" and then apply "kill -9 PID". These commands can easily be translated to python.

您需要查找进程 ID (pid)。您可以使用命令“ps -A | grep NAME”,然后应用“kill -9 PID”。这些命令可以很容易地转换为 python。

Trying to use a "name" (as in pkill) can yield multiple matches and thus unexpected results (at least in the context set above in the question).

尝试使用“名称”(如在 pkill 中)可能会产生多个匹配项,从而产生意想不到的结果(至少在问题上面设置的上下文中)。

回答by Michael Dillon

Try this:

试试这个:

echo '#!/usr/bin/env python' > myscript
cat myscript.py >>myscript
chmod +x myscript
./myscript

Of course you will have to change the code to kill a process named "myscript" On UNIX systems, an executable file contains a few bytes at the beginning which tell the OS what binary format is being used. If the first two bytes are #!then the OS assumes that this is actually a text file which can be executed by another program, and the OS loads the other program and passes to it, the text file.

当然,您必须更改代码以终止名为“myscript”的进程。在 UNIX 系统上,可执行文件的开头包含几个字节,用于告诉操作系统正在使用哪种二进制格式。如果前两个字节是,#!则操作系统假定这实际上是一个可由另一个程序执行的文本文件,并且操作系统加载另一个程序并将文本文件传递给它。

In this case I probably could have written #!/usr/bin/pythonon the top line, but if your python is in /usr/local/bin, then it would not work. Instead, I leverage envto get it to search your normal path for python. All UNIX systems have env in /usr/bin. For a bit more info you can type in man env.

在这种情况下,我可能会写#!/usr/bin/python在第一行,但是如果您的 python 位于/usr/local/bin. 相反,我利用env它来搜索 python 的正常路径。所有 UNIX 系统在 /usr/bin 中都有 env。有关更多信息,您可以输入man env.