How do I kill the last spawned background task in linux?
Example:
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
????
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
: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 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.
There's a special variable for this in bash:
$! expands to the PID of the last process executed in the background.
This should kill all background processes:
skill
is a version of the kill command that lets you select one or multiple processes based on a given criteria.