Skip to content

Instantly share code, notes, and snippets.

@MarkusMattinen
Last active January 24, 2017 13:39
Show Gist options
  • Save MarkusMattinen/f792f2d00c525c2c0646188b22e5ded6 to your computer and use it in GitHub Desktop.
Save MarkusMattinen/f792f2d00c525c2c0646188b22e5ded6 to your computer and use it in GitHub Desktop.
Kill and wait for children in shell scripts
#!/bin/bash
trap 'kill 0; wait' EXIT
./zombie.sh 5 &
./zombie.sh 10 &
./zombie.sh 5
#!/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
#!/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