当前位置: > Linux集群 > 负载均衡SLB >

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

时间:2014-12-14 22:01来源:linux.it.net.cn 作者:IT

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. If the layout gets funky, download the file directly: haproxy.init.

#!/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 http://mattiasgeniar.be/downloads/haproxy/haproxy.init -O /etc/init.d/haproxy

And modify it as you see fit.

Links:http://mattiasgeniar.be/2010/11/04/a-custom-init-d-start-up-script-for-haproxy-start-stop-restart-reload-checkconfig/


 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容