Shell – Terminate every background process

background-processkillshell

I have a few Stopped background processes.

kill $(jobs -p) and kill `jobs -p` have no effect

kill %1, kill %2, etc. successfully terminate individual processes

How can I kill every background process with one command?

Also, why do the first two commands not work for me?

I'm running Linux Mint 15, 64 bit

Best Answer

When they're running

Seems like you can just do this with kill and the output of jobs -p.

Example

$ sleep 1000 &
[1] 21952
$ sleep 1000 &
[2] 21956
$ sleep 1000 &
[3] 21960

Now I have 3 fake jobs running.

$ jobs
[1]   Running                 sleep 1000 &
[2]-  Running                 sleep 1000 &
[3]+  Running                 sleep 1000 &

Kill them all like so:

$ kill $(jobs -p)
[1]   Terminated              sleep 1000
[2]-  Terminated              sleep 1000
[3]+  Terminated              sleep 1000

Confirming they're all gone.

$ jobs
$

When they're stopped

If you have jobs that are stopped, not running you do this instead.

Example

$ kill $(jobs -p)

$ jobs
[1]+  Stopped                 sleep 1000
[2]-  Stopped                 sleep 1000
[3]   Stopped                 sleep 1000

OK so that didn't kill them, but that's because the kill signal cannot be handled by the process itself, it's stopped. So tell the OS to do the killing instead. That's what a -9 is for.

$ kill -9 $(jobs -p)
[1]+  Killed                  sleep 1000
[2]-  Killed                  sleep 1000
[3]   Killed                  sleep 1000

That's better.

$ jobs
$ 

When some are running and some are stopped

If you have a mixed bag of processes where some are stopped and some are running you can do a kill first followed by a kill -9.

$ kill $(jobs -p); sleep <time>; \
    kill -18 $(jobs -p); sleep <time>; kill -9 $(jobs -p)

Extending the time slightly if you need more to allow for processes to stop themselves first.

Signals

Neither a HUP (-1) or a SIGTERM (-15) to kill will succeed. But why? That's because these signals are kinder in the sense that they're telling the application to terminate itself. But since the application is in a stopped state it can't process these signals. So you're only course is to use a SIGKILL (-9).

You can see all the signals that kill provides with kill -l.

$ kill -l | column -t
1)   SIGHUP       2)   SIGINT       3)   SIGQUIT      4)   SIGILL       5)   SIGTRAP
6)   SIGABRT      7)   SIGBUS       8)   SIGFPE       9)   SIGKILL      10)  SIGUSR1
11)  SIGSEGV      12)  SIGUSR2      13)  SIGPIPE      14)  SIGALRM      15)  SIGTERM
16)  SIGSTKFLT    17)  SIGCHLD      18)  SIGCONT      19)  SIGSTOP      20)  SIGTSTP
21)  SIGTTIN      22)  SIGTTOU      23)  SIGURG       24)  SIGXCPU      25)  SIGXFSZ
26)  SIGVTALRM    27)  SIGPROF      28)  SIGWINCH     29)  SIGIO        30)  SIGPWR
31)  SIGSYS       34)  SIGRTMIN     35)  SIGRTMIN+1   36)  SIGRTMIN+2   37)  SIGRTMIN+3
38)  SIGRTMIN+4   39)  SIGRTMIN+5   40)  SIGRTMIN+6   41)  SIGRTMIN+7   42)  SIGRTMIN+8
43)  SIGRTMIN+9   44)  SIGRTMIN+10  45)  SIGRTMIN+11  46)  SIGRTMIN+12  47)  SIGRTMIN+13
48)  SIGRTMIN+14  49)  SIGRTMIN+15  50)  SIGRTMAX-14  51)  SIGRTMAX-13  52)  SIGRTMAX-12
53)  SIGRTMAX-11  54)  SIGRTMAX-10  55)  SIGRTMAX-9   56)  SIGRTMAX-8   57)  SIGRTMAX-7
58)  SIGRTMAX-6   59)  SIGRTMAX-5   60)  SIGRTMAX-4   61)  SIGRTMAX-3   62)  SIGRTMAX-2
63)  SIGRTMAX-1   64)  SIGRTMAX

If you want to learn even more about the various signals I highly encourage one to take a look at the signals man page,man 7 signal.

Related Question