Skip to content

Instantly share code, notes, and snippets.

@agriffis
Created March 27, 2014 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agriffis/9798319 to your computer and use it in GitHub Desktop.
Save agriffis/9798319 to your computer and use it in GitHub Desktop.
Test script for signal handling in heroku-buildpack-pgbouncer
#!/bin/bash
psmgr=/tmp/waiting-fifo
rm -f $psmgr
mkfifo $psmgr
pids=( $$ )
(
echo one-start
trap '' SIGTERM
sleep 101 &
trap "echo killing $!; kill -SIGINT $!" SIGHUP
wait
echo one-end
echo one >$psmgr
) &
pids+=( $! )
(
echo two-start
trap '' SIGTERM
sleep 102 &
trap "echo killing $!; kill -SIGINT $!" SIGHUP
wait
echo two-end
echo two >$psmgr
) &
pids+=( $! )
(
echo app-start
sleep 100 &
trap "kill $!" SIGTERM
echo app-wait
wait
echo app-end
echo app >$psmgr
) &
pids+=( $! )
sleep 1 # let everything start
# gather the inner pids
pids+=(
$(pgrep -f 'sleep 100')
$(pgrep -f 'sleep 101')
$(pgrep -f 'sleep 102')
)
# protect the top-level script
trap '' SIGTERM
case ${1:-app} in
all)
kill -${2:-SIGTERM} "${pids[@]}"
;;
one)
# kill aux one
kill -${2:-SIGTERM} %1
;;
one-sleep)
pkill -${2:-SIGTERM} -f 'sleep 101'
;;
two)
# kill aux two
kill -${2:-SIGTERM} %2
;;
two-sleep)
pkill -${2:-SIGTERM} -f 'sleep 102'
;;
app)
# kill the app
kill -${2:-SIGTERM} %3
;;
esac
read proc <$psmgr
rm -f $psmgr
echo "psmgr: $proc exited"
set -x
kill %3
wait %3
pkill -SIGHUP -f 'sleep 102'
pkill -SIGHUP -f 'sleep 101'
wait %2
wait %1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment