Coder Perfect

linux: terminate the background task


In Linux, how do I stop the last created background task?


doB &
#kill doB

Asked by flybywire

Solution #1

You can kill people based on their job number. You’ll see something like this when you place a task in the background:

$ ./script &
[1] 35341

The job number is [1], and it can be referenced as follows:

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

Use the jobs command to see a list of job numbers. More from the man bash crew:

Answered by John Kugelman

Solution #2

In bash, there’s a particular variable for this:

kill $!

$! expands to the PID of the most recent background process.

Answered by falstro

Solution #3

The command below displays a list of all background processes in your session, along with their pids. After that, you can utilize it to terminate the process.

jobs -l

Example usage:

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

Answered by Dave Vogt

Solution #4

All background processes should be terminated as a result of this:

jobs -p | xargs kill -9

Answered by Prabhu Are

Solution #5

skill doB

skill is a variant of kill that allows you to choose one or more processes based on a set of criteria.

Answered by gte525u

Post is based on