Skip to content

Instantly share code, notes, and snippets.

@jefferai jefferai/reload_haproxy.sh
Last active Oct 22, 2015

Embed
What would you like to do?
haproxy reloader
#!/bin/bash
haproxy_bin="/usr/sbin/haproxy"
haproxy_config="/etc/haproxy/haproxy.cfg"
haproxy_tmp_config=$(mktemp --tmpdir haproxy_config_XXXX)
haproxy_pidfile="/run/haproxy.pid"
haproxy_temp_pidfile="/run/haproxy-reloader-${BASHPID}.pid"
EXTRAOPTS=
if [ -e /etc/default/haproxy ]; then
. /etc/default/haproxy
fi
[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions
log_daemon_msg "Starting reload script at $(date -R)"
(( acquireloops = 5 ))
while (( acquireloops > 0 )); do
log_daemon_msg "Moving pidfile"
mv $haproxy_pidfile $haproxy_temp_pidfile
if [[ $? -ne 0 ]]; then
(( acquireloops -= 1 ))
log_daemon_msg "Reload in progress or haproxy not running, retrying in one second"
sleep 1
else
log_daemon_msg "Acquired pidfile"
break
fi
done
if (( acquireloops == 0 )); then
log_daemon_msg "Error getting pid file"
log_end_msg 3
exit 3
fi
(( configloops = 5 ))
while (( configloops > 0 )); do
log_daemon_msg "Copying and checking config"
cp $haproxy_config $haproxy_tmp_config
$haproxy_bin -c -f "$haproxy_tmp_config" >/dev/null
if [[ $? -eq 1 ]]; then
(( configloops -= 1 ))
log_daemon_msg "Error in haproxy config, looping"
sleep 1
else
log_daemon_msg "Got valid haproxy config"
break
fi
done
if (( configloops == 0 )); then
log_daemon_msg "Error getting valid config; moving pidfile back"
mv $haproxy_temp_pidfile $haproxy_pidfile
log_end_msg 4
exit 4
fi
log_daemon_msg "Reloading haproxy"
$haproxy_bin -f "$haproxy_tmp_config" -p $haproxy_temp_pidfile -D $EXTRAOPTS -sf $(cat $haproxy_temp_pidfile)
if [[ $? -ne 0 ]]; then
log_daemon_msg "Error reloading haproxy"
log_end_msg 2
exit 2
fi
# Give haproxy time to actually read the pid file before moving it
sleep 1
log_daemon_msg "Moving pidfile back"
mv $haproxy_temp_pidfile $haproxy_pidfile
if [[ $? -ne 0 ]]; then
log_daemon_msg "Error moving pidfile back"
log_end_msg 3
exit 3
fi
log_end_msg 0
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.