3/03/2023

run shell script in parallel and kill all shell when it stop

 refer to shell script



.

#!/bin/bash

# Run each script in the background
./loop_invocations.sh &
./loop_invocations.sh &
./loop_invocations.sh &
./loop_invocations.sh &

./loop_ping.sh &

# Define a function to kill all child processes
function kill_children {
echo "Stopping all child processes..."
pkill -P $$
exit 0
}

# Trap the SIGINT signal and call the function to kill child processes
trap kill_children SIGINT

# Wait for all background jobs to complete
wait

..




In this example, the kill_children function uses pkill to find and kill all child processes of the current process. The $$ variable represents the PID of the current process. The trap command sets up a handler for the SIGINT signal that calls the kill_children function. When the script is stopped with CTRL+C, the kill_children function is called, which kills all child processes before exiting the script.




Thank you.

www.marearts.com

No comments:

Post a Comment