Created
September 19, 2015 00:07
-
-
Save shanemikel/51fa4172896e53c8dc0a to your computer and use it in GitHub Desktop.
out.txt is what my shell looks like, trap.sh receives SIGINTS, sending a SIGTERM to the most recently forked loop.sh
This file contains hidden or 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 'echo "loop $$ recieved SIGTERM; exiting" && exit' SIGTERM | |
for i in `seq $1 -1 1`; do | |
echo count = $i in loop $$ | |
sleep 1s | |
done |
This file contains hidden or 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
chronos@localhost ~ $ sudo enter-chroot ./trap.sh | |
Entering /mnt/stateful_partition/crouton/chroots/unity... | |
count = 6 in loop 2819 | |
count = 4 in loop 2817 | |
count = 5 in loop 2818 | |
count = 5 in loop 2819 | |
count = 4 in loop 2818 | |
count = 3 in loop 2817 | |
^CTRAP SIGINT, sending TERM to 2819 | |
2815 2819 /bin/bash ./loop.sh 6 | |
loop 2819 recieved SIGTERM; exiting | |
count = 3 in loop 2818 | |
count = 2 in loop 2817 | |
^CTRAP SIGINT, sending TERM to 2818 | |
2815 2818 /bin/bash ./loop.sh 5 | |
loop 2818 recieved SIGTERM; exiting | |
count = 1 in loop 2817 | |
Not unmounting /mnt/stateful_partition/crouton/chroots/unity as another instance is using it. | |
chronos@localhost ~ $ |
This file contains hidden or 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_int() { | |
index=$((${#loop[@]}-1)) | |
echo TRAP SIGINT, sending TERM to ${loop[$index]} >&2 | |
ps -o ppid= -o pid= -o cmd= ${loop[$index]} | |
kill ${loop[$index]} | |
unset loop[$index] | |
} | |
all_done() { | |
if [ ${#loop[@]} -eq 0 ]; then | |
return 0 | |
else | |
for l in ${loop[@]}; do | |
kill -0 $l &>/dev/null && return 1 | |
done | |
return 0 | |
fi | |
} | |
trap 'trap_int' SIGINT | |
for i in `seq 3`; do | |
./loop.sh $((i+3)) & loop[$((i-1))]=$! | |
done | |
while true; do | |
all_done && sleep 2s && exit | |
sleep 0.5s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment