A custom init.d start-up script for HAProxy: start, stop, restart, reload, checkconfig

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, November 04, 2010

Follow me on Twitter as @mattiasgeniar

I created the following init.d script for HAProxy, which will allow you to add it to the automatic start-up, and perform a start, stop, restart, reload and checkconfig on it.

The reload option was created based on the excellent post titled “HAProxy: Reloading Your Config With Minimal Service Impact", by Michael Goffin.

Here’s how to add it in your auto start-up, once the file is in the /etc/init.d directory.

$ chkconfig --add haproxy
$ chkconfig haproxy on

And here’s the init.d script itself. You might have to change the paths to the binary and config file, depending on your set-up.

#!/bin/sh
#
# custom haproxy init.d script, by Mattias Geniar
#
# haproxy         starting and stopping the haproxy load balancer
#
# chkconfig: 345 55 45
# description: haproxy is a TCP loadbalancer
# probe: true

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -f /usr/local/sbin/haproxy ] || exit 0

[ -f /etc/haproxy/haproxy.conf ] || exit 0

# Define our actions
checkconfig() {
    # Check the config file for errors
    /usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
    if [ $? -ne 0 ]; then
            echo "Errors found in configuration file."
            return 1
      fi

    # We're OK!
    return 0
}

start() {
    # Check config
    /usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

    echo -n "Starting HAProxy: "
    daemon /usr/local/sbin/haproxy -D -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid

    RETVAL=$?
    echo
      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
      return $RETVAL
}

stop() {
    echo -n "Shutting down HAProxy: "
      killproc haproxy -USR1

      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
      [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid
      return $RETVAL
}

restart() {
    /usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

    stop
      start
}

check() {
      /usr/local/sbin/haproxy -c -q -V -f /etc/haproxy/haproxy.conf
}

rhstatus() {
      status haproxy
}

reload() {
    /usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

    echo -n "Reloading HAProxy config: "
    /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

    success $"Reloading HAProxy config: "
    echo
}

# Possible parameters
case "$1" in
  start)
        start
    ;;
  stop)
    stop
        ;;
  status)
        rhstatus
        ;;
  restart)
    restart
        ;;
  reload)
        reload
        ;;
  checkconfig)
    check
    ;;
  *)
        echo "Usage: haproxy {start|stop|status|restart|reload|checkconfig}"
        exit 1
esac

exit 0

Or you can download it from this site.

$ wget /downloads/haproxy/haproxy.init -O /etc/init.d/haproxy

And modify it as you see fit.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.