Linux:杀死后台任务

发布于 2024-08-09 08:42:29 字数 125 浏览 8 评论 0原文

如何终止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雅心素梦 2024-08-16 08:42:29

你可以通过工作号码杀人。当您将任务置于后台时,您会看到类似以下内容:

$ ./script &
[1] 35341

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

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

要查看作业编号列表,请使用 jobs< /代码> 命令。更多来自 man bash

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

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:

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

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

There are a number of ways to refer to a job in the shell. The character % introduces a job name. Job number n may 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, %ce refers to a stopped ce job. 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 ce in 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.

喵星人汪星人 2024-08-16 08:42:29

Bash 中有一个特殊的变量:

kill $!

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

There's a special variable for this in Bash:

kill $!

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

自找没趣 2024-08-16 08:42:29

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

jobs -l

用法示例:

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

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.

jobs -l

Example usage:

$ sleep 300 &
$ jobs -l
[1]+ 31139 Running                 sleep 300 &
$ kill 31139
锦上情书 2024-08-16 08:42:29

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

jobs -p | xargs kill -9

This should kill all background processes:

jobs -p | xargs kill -9
薄荷→糖丶微凉 2024-08-16 08:42:29
skill doB

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

skill doB

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

时光与爱终年不遇 2024-08-16 08:42:29

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

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

葮薆情 2024-08-16 08:42:29

正如 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.

手长情犹 2024-08-16 08:42:29

只需使用killall命令:

killall任务名称

以获取更多信息和更高级的选项,输入“mankillall”。

Just use the killall command:

killall taskname

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文