Linux:杀死后台任务
如何终止 Linux 中最后生成的后台任务?
例子:
doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
How do I kill the last spawned background task in Linux?
Example:
doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你可以通过工作号码杀人。当您将任务置于后台时,您会看到类似以下内容:
[1]
是作业编号,可以像这样引用:要查看作业编号列表,请使用
jobs< /代码> 命令。更多来自
man bash
:You can kill by job number. When you put a task in the background you'll see something like:
That
[1]
is the job number and can be referenced like:To see a list of job numbers use the
jobs
command. More fromman bash
:Bash 中有一个特殊的变量:
$!扩展为后台执行的最后一个进程的 PID。
There's a special variable for this in Bash:
$! expands to the PID of the last process executed in the background.
以下命令为您提供会话中所有后台进程的列表以及 pid。然后您可以使用它来终止该进程。
用法示例:
The following command gives you a list of all background processes in your session, along with the pid. You can then use it to kill the process.
Example usage:
这应该杀死所有后台进程:
This should kill all background processes:
skill
是kill 命令的一个版本,可让您根据给定条件选择一个或多个进程。skill
is a version of the kill command that lets you select one or multiple processes based on a given criteria.您需要它的 pid...使用“ps -A”找到它。
You need its pid... use "ps -A" to find it.
正如 John Kugelman 的回答所示,
%
是相关的到工作规范。我们怎样才能有效地找到它?使用 less 的 &pattern 命令。似乎 man 使用 less 寻呼机(我不太确定)。在“man”Bash 中,键入 &%,然后键入 Enter。它只会显示包含“%”的行。要重新显示全部,请输入 &。然后输入。
As in John Kugelman's answer,
%
is related to job specification.How can we efficiently find that? Use less's &pattern command. It seems man uses the less pager (I am not that sure). In 'man' Bash, type &%, and then type Enter. It will only show lines that containing '%'. To reshow all, type &. And then Enter.
只需使用killall命令:
killall任务名称
以获取更多信息和更高级的选项,输入“mankillall”。
Just use the killall command:
killall taskname
for more info and more advanced options, type "man killall".