Created
October 7, 2014 20:24
-
-
Save sheldonh/39c1c8e264400420ccd4 to your computer and use it in GitHub Desktop.
follow etcd leader
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/sh | |
kill_daemon() { | |
pid=$(getpid) | |
if [ -n "$pid" ]; then | |
echo "DEBUG: killing $pid" 1>&2 | |
kill $pid | |
while kill -0 $pid 2>/dev/null; do | |
if ! sleep 1; then | |
trap EXIT | |
rm -f $pidfile | |
exit 1 | |
fi | |
done | |
fi | |
} | |
getpid() { | |
cat $pidfile | |
} | |
setpid() { | |
pid=$(jobs -p) | |
if [ -n "$pid" ]; then | |
echo "DEBUG: $pid" 1>&2 | |
echo $pid > $pidfile | |
fi | |
} | |
cleanup() { | |
kill_daemon | |
rm -f $pidfile | |
} | |
trap cleanup EXIT | |
pidfile=$(mktemp) | |
while true; do echo no redis leader; sleep 1; done & | |
setpid | |
bin/etcdctl watch --forever /leader | while read leader; do | |
kill_daemon | |
while true; do echo redis leader: $leader; sleep 1; done & | |
setpid | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment