linux:杀死后台任务

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

linux: kill background task

linuxbashunixkilljob-control

提问by flybywire

How do I kill the last spawned background task in linux?

如何终止 Linux 中最后一个生成的后台任务?

Example:

例子:

doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????

采纳答案by falstro

There's a special variable for this in bash:

在 bash 中有一个特殊的变量:

kill $!

$! expands to the PID of the last process executed in the background.

$! 扩展为在后台执行的最后一个进程的 PID。

回答by jldupont

You need its pid... use "ps -A" to find it.

你需要它的pid...使用“ps -A”来找到它。

回答by zakk

Just use the killall command:

只需使用 killall 命令:

killall taskname

killall 任务名称

for more info and more advanced options, type "man killall".

有关更多信息和更多高级选项,请键入“man killall”。

回答by gte525u

skill doB

skillis a version of the kill command that lets you select one or multiple processes based on a given criteria.

skill是 kill 命令的一个版本,它允许您根据给定的条件选择一个或多个进程。

回答by John Kugelman

You can kill by job number. When you put a task in the background you'll see something like:

您可以按工作编号杀死。当您将任务置于后台时,您会看到如下内容:

$ ./script &
[1] 35341

That [1]is the job number and can be referenced like:

[1]是工作编号,可以像这样引用:

$ kill %1
$ kill %%  # Most recent background job

To see a list of job numbers use the jobscommand. More from man bash:

要查看作业编号列表,请使用该jobs命令。更多来自man bash

There are a number of ways to refer to a job in the shell. The character %introduces a job name. Job number nmay be referred to as %n. A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, %cerefers to a stopped cejob. If a prefix matches more than one job, bash reports an error. Using %?ce, on the other hand, refers to any job containing the string cein its command line. If the substring matches more than one job, bash reports an error. The symbols %%and %+refer to the shell's notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using %-. In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a +, and the previous job with a -. A single %(with no accompanying job specification) also refers to the current job.

有多种方法可以在 shell 中引用作业。该字符%引入了工作名称。作业号n可称为%n。还可以使用用于启动它的名称的前缀或使用出现在其命令行中的子字符串来引用作业。例如,%ce指停止的ce作业。如果前缀匹配多个作业,bash 会报告错误。使用%?ce,在另一方面,是指包含字符串的任何作业ce在其命令行。如果子字符串匹配多个作业,bash 会报告错误。符号%%%+指的是 shell 对当前作业的概念,它是在前台或后台启动时停止的最后一个作业。可以使用 引用以前的作业%-。在与作业有关的输出中(例如,jobs 命令的输出),当前作业始终标记为+,而前一个作业标记为-。单个%(没有附带的工作规范)也指当前的工作。

回答by Dave Vogt

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.

以下命令为您提供会话中所有后台进程的列表以及 pid。然后,您可以使用它来终止该进程。

jobs -l

Example usage:

用法示例:

$ sleep 300 &
$ jobs -l
[1]+ 31139 Running                 sleep 300 &
$ kill 31139

回答by Prabhu Are

This should kill all background processes:

这应该会杀死所有后台进程:

jobs -p | xargs kill -9

回答by qeatzy

this is an out of topic answer, but, for those who are interested, it maybe valuable.

这是一个题外话的答案,但是,对于那些感兴趣的人来说,它可能很有价值。

As in @John Kugelman's answer, % is related to job specification. how to efficiently find that? use less's &pattern command, seems man use less pager (not that sure), in man bash type &% then type Enter will only show lines that containing '%', to reshow all, type &. then Enter.

正如@John Kugelman 的回答一样, % 与工作规范有关。 如何有效地找到它?使用 less 的 &pattern 命令,似乎 man 使用 less pager(不太确定),在 man bash 中键入 &% 然后键入 Enter 将只显示包含“%”的行,要重新显示所有内容,请键入 &。然后输入。