How to kill all subprocesses of shell? How to kill all subprocesses of shell? bash bash

How to kill all subprocesses of shell?


pkill -P $$

will fit (just kills it's own descendants)

EDIT: I got a downvote, don't know why. Anyway here is the help of -P

   -P, --parent ppid,...          Only match processes whose parent process ID is listed.

and $$ is the process id of the script itself


After starting each child process, you can get its id with

ID=$!

Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.


If you use a negative PID with kill it will kill a process group. Example:

kill -- -1234