Search code examples
linuxbashunixkilljob-control

Linux: kill background task


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

Example:

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

Solution

  • There's a special variable for this in Bash:

    kill $!
    

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