我如何杀死最后一个衍生的后台任务在linux?
例子:
doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
我如何杀死最后一个衍生的后台任务在linux?
例子:
doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
当前回答
下面的命令为您提供会话中所有后台进程的列表,以及pid。然后您可以使用它来终止进程。
jobs -l
使用示例:
$ sleep 300 &
$ jobs -l
[1]+ 31139 Running sleep 300 &
$ kill 31139
其他回答
下面的命令为您提供会话中所有后台进程的列表,以及pid。然后您可以使用它来终止进程。
jobs -l
使用示例:
$ sleep 300 &
$ jobs -l
[1]+ 31139 Running sleep 300 &
$ kill 31139
在bash中有一个特殊的变量:
kill $!
美元!扩展为最后一个在后台执行的进程的PID。
你可以通过工号来杀人。当你把一个任务放在后台时,你会看到如下内容:
$ ./script &
[1] 35341
[1]是工作编号,可以这样引用:
$ kill %1
$ kill %% # Most recent background job
要查看作业号列表,请使用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 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.
使用killall命令:
killall taskname
更多信息和更高级的选项,键入“man killall”。
skill doB
Skill是kill命令的一个版本,允许您根据给定的标准选择一个或多个进程。