My bash script named n_commands.sh is of the form
command1 &
command2 &
..........
commandn &
When I run bash n_commands.sh on the terminal then all the commands are running in the background.
When I try to stop all the n processes that are using the resources, I need to kill each process separately. Is there any way to stop all the background processes that are running due to the command bash n_commands.sh instantly without killing them manually?
CodePudding user response:
After you start all the background processes, have the main script wait for them to finish. Then you can kill the entire process group of the main script, and all the child processes will be killed as well.
command1 &
command2 &
..........
commandn &
wait
After you start this, if you type Control-c it will kill all the commands.
CodePudding user response:
You can use pkill to kill all children of the current process.
p=$$
command1 &
command2 &
..........
commandn &
...
pkill -P $p
