在 bash 中杀死一个进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2775009/
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-09-09 19:15:36  来源:igfitidea点击:

kill a process in bash

bash

提问by wyatt

How do I kill a process which is running in bash - for example, suppose I open a file:

如何终止在 bash 中运行的进程 - 例如,假设我打开一个文件:

$ gedit file.txt

$ gedit 文件.txt

is there any way within the command prompt to close it? This example is fairly trivial, since I could just close the window, but it seems to come up a bit, particularly when I mistype commands.

命令提示符中有什么方法可以关闭它吗?这个例子相当简单,因为我可以关闭窗口,但它似乎出现了一点,特别是当我输入错误命令时。

Also is there any way to escape an executable which is running? This probably has the same solution, but I thought I'd ask anyway.

还有什么办法可以转义正在运行的可执行文件?这可能有相同的解决方案,但我想我还是会问。

Thanks

谢谢

采纳答案by jweyrich

To interrupt it, you can try pressing ctrlcto send a SIGINT. If it doesn't stop it, you may try to kill it using kill -9 <pid>, which sends a SIGKILL. The latter can't be ignored/intercepted by the process itself (the one being killed).

要中断它,您可以尝试按ctrlc发送 SIGINT。如果它没有阻止它,您可以尝试使用kill -9 <pid>发送 SIGKILL 的来杀死它。后者不能被进程本身忽略/拦截(被杀死的那个)。

To move the active process to background, you can press ctrlz. The process is sent to background and you get back to the shell prompt. Use the fgcommand to do the opposite.

要将活动进程移至后台,您可以按ctrlz。该进程被发送到后台,您将返回到 shell 提示符。使用该fg命令执行相反的操作。

回答by tanascius

You have a multiple options:

您有多种选择:

First, you can use kill. But you need the pid of your process, which you can get by using ps, pidofor pgrep.

首先,您可以使用kill。但是您需要进程的 pid,您可以使用pspidofpgrep 获得

ps -A  // to get the pid, can be combined with grep
-or-
pidof <name>
-or-
pgrep <name>

kill <pid>

It is possible to kill a process by just knowing the name. Use pkillor killall.

只需知道名称就可以终止进程。使用pkillkillall

pkill <name>
-or-
killall <name>

All commands send a signal to the process. If the process hung up, it might be neccessary to send a sigkillto the process (this is signal number 9, so the following examples do the same):

所有命令都向进程发送信号。如果进程挂了,则可能需要向进程发送一个sigkill(这是信号编号 9,因此以下示例执行相同操作):

pkill -9 <name>
pkill -SIGKILL <name>

You can use this option with killand killall, too.

您也可以将此选项与kill和一起使用killall

Read this article about controlling processesto get more informations about processes in general.

阅读这篇关于控制进程的文章,以获取有关进程的更多信息。

回答by Liam

try kill -9 {processID}

尝试 kill -9 {processID}

To find the process ID you can use ps -ef | grep gedit

要查找您可以使用的进程 ID ps -ef | grep gedit

回答by Ian

Old post, but I just ran into a very similar problem. After some experimenting, I found that you can do this with a single command:

旧帖子,但我遇到了一个非常相似的问题。经过一些试验,我发现你可以用一个命令来做到这一点:

kill $(ps aux | grep <process_name> | grep -v "grep" | cut -d " " -f2)

In OP's case, <process_name>would be "gedit file.txt".

在 OP 的情况下,<process_name>将是"gedit file.txt".

回答by Yannuth

This one is violent use with caution :

这是谨慎使用的暴力:

pkill -9 -e -f processname 

If your process name is "sh" it will also kill "bash"

如果您的进程名称是“sh”,它也会杀死“bash”

回答by AllenJB

You can use the command pkill to kill processes. If you want to "play around", you can use "pgrep", which works exactly the same but returns the process rather than killing it.

您可以使用命令 pkill 来终止进程。如果你想“玩转”,你可以使用“pgrep”,它的工作原理完全相同,但返回进程而不是杀死它。

pkill has the -f parameter that allows you to match against the entire command. So for your example, you can: pkill -f "gedit file.txt"

pkill 具有 -f 参数,允许您匹配整个命令。因此,对于您的示例,您可以: pkill -f "gedit file.txt"

回答by GreenMatt

It is not clear to me what you mean by "escape an executable which is running", but ctrl-z will put a process into the background and return control to the command line. You can then use the fg command to bring the program back into the foreground.

我不清楚“转义正在运行的可执行文件”是什么意思,但是 ctrl-z 会将进程置于后台并将控制权返回给命令行。然后您可以使用 fg 命令将程序带回前台。

回答by ganesh

Please check "top" command then if your script or any are running please note 'PID'

请检查“top”命令,然后如果您的脚本或任何脚本正在运行,请注意“PID”

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1384 root      20   0  514m  32m 2188 S  0.3  5.4  55:09.88 example
14490 root      20   0 15140 1216  920 R  0.3  0.2   0:00.02 example2



kill <you process ID>

Example : kill 1384