public
Last active

Setting up haproxy as a daemon on your ubuntu server

  • Download Gist
haproxyd.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#!/bin/bash
 
CONFIG=/etc/haproxy/haproxy.cfg
DAEMON=$(which haproxy)
 
function get_pid() {
PID=$(ps aux | grep haproxy | grep -v grep | awk '{print $2}')
}
 
function get_status() {
get_pid
 
if [ -z $PID ]; then
echo 'not running'
else
echo "running with pid: $PID"
fi
}
 
case $1 in
start)
echo -n 'starting haproxy '
$DAEMON -f $CONFIG
get_status
;;
restart)
echo -n 'restarting haproxy '
get_pid
$DAEMON -f $CONFIG -sf $PID
get_status
;;
stop)
echo -n 'stopping haproxy '
get_pid
kill -9 $PID
get_status
;;
status)
echo -n 'haproxy is '
get_status
;;
*)
echo "Usage: haproxyd {start|restart|stop|status}" >&2
exit 1
;;
esac
 
exit 0

Once the script above is available on /etc/init.d do the following (Ubuntu/Debian)

cd /etc/init.d
sudo update-rc.d haproxyd defaults

then you can

$ sudo service haproxyd status
haproxy is running with pid: 4498

root@ipXX:/etc/init.d# ./haproxy status
haproxy is ./haproxy: line 13: [: 9060: binary operator expected
running with pid: 9060
9062

:(

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.