From https://unix.stackexchange.com/a/43709/674
To just
kill
all background jobs managed bybash
, dokill $(jobs -p)
Does kill $(jobs -p)
kill only those "running" background jobs in the current shell, but not those background jobs in the shell which are in "stopped" or some other states?
How can we kill all the background jobs in the current shell, regardless of whether they are in running, stopped or some other states?
For example, I created two stopped jobs, by running less
and then ctrl+z
:
$ ps | less
[1]+ Stopped ps | less
$ less recover_jobs.sh
[2]+ Stopped less recover_jobs.sh
$ jobs -l
[1]- 2753 Done ps
2754 Stopped | less
[2]+ 2842 Stopped less recover_jobs.sh
$ jobs -p
2753
2842
$ kill $(jobs -p)
bash: kill: (2753) - No such process
$ kill 2754
$ kill 2842
$ jobs -l
[1]- 2753 Done ps
2754 Stopped | less
[2]+ 2842 Stopped less recover_jobs.sh
$ fg 1
ps | less
Terminated
$ fg 2
less recover_jobs.sh
Terminated
I was wondering why:
kill $(jobs -p)
doesn't kill anystopped
job,jobs -p
doesn't show the pid 2754 of the stopped job| less
, but the pid 2753 of the done jobps
,kill 2754
andkill 2842
don't kill the stopped jobs for| less
and forless recover_jobs.sh
, and- when I
fg
the stopped jobs, they are immediately shownTerminated
but not before Ifg
them?
Thanks.
jobs -p | xargs -I{} kill -9 -{}
might do a better job at killing all a shell's background processes.