Skip to content

Instantly share code, notes, and snippets.

@wbailey
Created December 22, 2011 06:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbailey/1509227 to your computer and use it in GitHub Desktop.
Save wbailey/1509227 to your computer and use it in GitHub Desktop.
Setting up haproxy as a daemon on your ubuntu server
#!/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
@wbailey
Copy link
Author

wbailey commented Dec 22, 2011

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

@sardaukar
Copy link

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

:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment