flybywire flybywire - 5 months ago 29
Linux Question

linux: kill background task

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

Example:

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

Answer

There's a special variable for this in bash:

kill $!

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