Last active
January 24, 2017 13:39
-
-
Save MarkusMattinen/f792f2d00c525c2c0646188b22e5ded6 to your computer and use it in GitHub Desktop.
Kill and wait for children in shell scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
trap 'kill 0; wait' EXIT | |
./zombie.sh 5 & | |
./zombie.sh 10 & | |
./zombie.sh 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
./supervisor.sh & | |
SUPERVISOR_PID=$! | |
wait_for_children() { | |
echo "Zombies running:" | |
ps aux | grep zombie | |
echo "Waiting for supervisor to exit." | |
wait $SUPERVISOR_PID | |
echo "Zombies running:" | |
ps aux | grep zombie | |
EPOCH=$(date +%s) | |
WAIT_SECONDS=$(( $EPOCH - $KILL_EPOCH )) | |
echo "Waited $WAIT_SECONDS seconds" | |
} | |
trap wait_for_children EXIT | |
sleep 1 | |
echo "Zombies running:" | |
ps aux | grep zombie | |
sleep 1 | |
KILL_EPOCH=$(date +%s) | |
echo "Killing supervisor." | |
kill $SUPERVISOR_PID | |
# Catch signal from supervisor | |
sleep 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DELAY=$1 | |
trap "exec -a zombie_delayer sleep $DELAY" EXIT | |
(exec -a zombie_sleeper sleep 60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment