As others have said already, jobs -p | xargs kill
or kill $(jobs -p)
is wrong as:
jobs -p
prints the process group ids, with extra information with zsh
kill number
kills the process with number as id, not process group. There's not even a guarantee that the process group ids returned by jobs -p
will have a corresponding running process by the same number. And even if there is, kill will not kill the other processes in the job / process-group
- if there's no job in the job table,
kill
will be run without arguments causing an error.
Here you want to run either kill -- -<pgid>
or kill %<jobnumber>
for all the jobs.
With bash
or other shells that only print the pgid upon jobs -p
, you can do:
jobs -p | sed 's/^/-/' | xargs -r kill --
(-r
being a GNU extension)
Note that it doesn't work in dash
where jobs
running in a subshell (because as part of a pipeline) only lists the jobs of that subshell. There, you could instead redirect the output of jobs -p
to a temporary file and then run sed|xargs
on it, but anyway dash
is not really intended to be run interactively.
With zsh
:
() { (($#)) && kill %${^@}; } ${(k)jobstates}
Where we pass the list of job numbers (the k
eys of the $jobstates
special associative array) to an anonymous function that runs kill with %
prepended to the job number if its number of arguments is non-zero.
kill $(jobs -p)
seems easier.for pid in $(jobs -p); do kill $pid; done
?jobs
which only works if the jobs happen to be numbered consecutively. Oh, and “kill jobs individually” is meaningless: passing multiple PIDs to thekill
command does exactly the same thing as passing them separately.