3

I'm using runit to manage an HAProxy and want to do a safe restart to reload a configuration file (specifically: haproxy -f /etc/haproxy/haproxy.cfg -sf $OLD_PROCESS_ID). I figure that I could run sv restart haproxy and tried to add a custom script named /etc/service/haproxy/restart, but it never seems to execute. How do I have a special restart script? Is my approach even good here? How do I reload my config with minimal impact using runit?

Nathan Perry
  • 291
  • 2
  • 11

1 Answers1

3

HAProxy runit service script

/etc/service/haproxy/run

#!/bin/sh
#
# runit haproxy
#

# forward stderr to stdout for use with runit svlogd
exec 2>&1

PID_PATH=/var/run/haproxy/haproxy.pid
BIN_PATH=/opt/haproxy/sbin/haproxy
CFG_PATH=/opt/haproxy/etc/haproxy.cfg

exec /bin/bash <<EOF
$BIN_PATH -f $CFG_PATH -D -p $PID_PATH

trap "echo SIGHUP caught; $BIN_PATH -f $CFG_PATH -D -p $PID_PATH -sf \\\$(cat $PID_PATH)" SIGHUP
trap "echo SIGTERM caught; kill -TERM \\\$(cat $PID_PATH) && exit 0" SIGTERM SIGINT

while true; do # Iterate to keep job running.
  sleep 1 # Wake up to handle signals
done
EOF

Graceful reload that keeps things up and running.

sv reload haproxy

Full stop and start.

sv restart haproxy

This solution was inspired by https://gist.github.com/gfrey/8472007

kevpie
  • 25,206
  • 2
  • 24
  • 28
  • Please let me know if there are any problems. I just figured this one out and haven't yet really put it through it's paces. – kevpie Apr 29 '15 at 06:56
  • The only real problem with it is the lack of logging, which can _probably_ be done by adding a line `exec 2>&1` before the `exec /bin/bash < – Lauri P Jun 02 '15 at 11:36
  • Thank you @LauriPiisang, I was using this in conjunction with *consul-template* and had another problem where I was having the trap called so quickly as I was scaling in backends (up to 30 at once) that I would have up to 5 *haproxy*'s reading and writing to the pidsfile at the same time. I ended up telling *consul-template* to ease up on how quickly it would restart *haproxy*. – kevpie Jun 02 '15 at 16:30