So, I have some jobs like this:
sleep 30 | sleep 30 &
The natural way to think would be:
kill `jobs -p`
But that kills only the first sleep
but not the second.
Doing this kills both processes:
kill %1
But that only kills at most one job if there are a lot of such jobs running.
It shouldn't kill processes with the same name but not run in this shell.
Best Answer
Use this:
jobs -p
prints the PID of process group leaders. By providing a negative PID tokill
, we kill all the processes belonging to that process group (man 2 kill
)."${pids[@]/#/-}"
just negates each PID stored in arraypids
.