Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: haproxy
- # Required-Start: $local_fs $network $remote_fs $syslog
- # Required-Stop: $local_fs $remote_fs $syslog
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: fast and reliable load balancing reverse proxy
- # Description: This file should be used to start and stop haproxy.
- ### END INIT INFO
- # Author: Arnaud Cornet <acornet@debian.org>
- PATH=/sbin:/usr/sbin:/bin:/usr/bin
- PIDFILE=/var/run/haproxy.pid
- CONFIGDIR=/etc/haproxy/conf.d/
- TMPCONFIG=/tmp/haproxy.tmp.conf
- HAPROXY=/usr/sbin/haproxy
- RUNDIR=/run/haproxy
- EXTRAOPTS=
- test -x $HAPROXY || exit 0
- if [ -e /etc/default/haproxy ]; then
- . /etc/default/haproxy
- fi
- test -d "$CONFIGDIR" || exit 0
- [ -f /etc/default/rcS ] && . /etc/default/rcS
- . /lib/lsb/init-functions
- check_haproxy_config()
- {
- files=`find /etc/haproxy/conf.d/ -name *.conf -printf "%p\n"`
- while read line; do
- cat $line >> "$TMPCONFIG"
- done <<< """$files"""
- $HAPROXY -c -f "$TMPCONFIG" >/dev/null
- if [ $? -eq 1 ]; then
- log_end_msg 1
- exit 1
- fi
- rm "$TMPCONFIG"
- }
- haproxy_start()
- {
- [ -d "$RUNDIR" ] || mkdir "$RUNDIR"
- chown haproxy:haproxy "$RUNDIR"
- chmod 2775 "$RUNDIR"
- check_haproxy_config
- files=`find /etc/haproxy/conf.d/ -name *.conf -printf " -f %p"`
- start-stop-daemon --quiet --oknodo --start --pidfile "$PIDFILE" \
- --exec $HAPROXY -- $files -D -p "$PIDFILE" \
- $EXTRAOPTS || return 2
- return 0
- }
- haproxy_stop()
- {
- if [ ! -f $PIDFILE ] ; then
- # This is a success according to LSB
- return 0
- fi
- for pid in $(cat $PIDFILE) ; do
- if kill -0 $pid 2>/dev/null; then
- /bin/kill $pid || return 4
- fi
- done
- rm -f $PIDFILE
- return 0
- }
- haproxy_reload()
- {
- check_haproxy_config
- files=`find /etc/haproxy/conf.d/ -name *.conf -printf "-f %p "`
- $HAPROXY "$files" -p $PIDFILE -D $EXTRAOPTS -sf $(cat $PIDFILE) \
- || return 2
- return 0
- }
- haproxy_status()
- {
- if [ ! -f $PIDFILE ] ; then
- # program not running
- return 3
- fi
- for pid in $(cat $PIDFILE) ; do
- if ! ps --no-headers p "$pid" | grep haproxy > /dev/null ; then
- # program running, bogus pidfile
- return 1
- fi
- done
- return 0
- }
- case "$1" in
- start)
- log_daemon_msg "Starting haproxy" "haproxy"
- haproxy_start
- ret=$?
- case "$ret" in
- 0)
- log_end_msg 0
- ;;
- 1)
- log_end_msg 1
- echo "pid file '$PIDFILE' found, haproxy not started."
- ;;
- 2)
- log_end_msg 1
- ;;
- esac
- exit $ret
- ;;
- stop)
- log_daemon_msg "Stopping haproxy" "haproxy"
- haproxy_stop
- ret=$?
- case "$ret" in
- 0|1)
- log_end_msg 0
- ;;
- 2)
- log_end_msg 1
- ;;
- esac
- exit $ret
- ;;
- reload|force-reload)
- log_daemon_msg "Reloading haproxy" "haproxy"
- haproxy_reload
- ret=$?
- case "$ret" in
- 0|1)
- log_end_msg 0
- ;;
- 2)
- log_end_msg 1
- ;;
- esac
- exit $ret
- ;;
- restart)
- log_daemon_msg "Restarting haproxy" "haproxy"
- haproxy_stop
- haproxy_start
- ret=$?
- case "$ret" in
- 0)
- log_end_msg 0
- ;;
- 1)
- log_end_msg 1
- ;;
- 2)
- log_end_msg 1
- ;;
- esac
- exit $ret
- ;;
- status)
- haproxy_status
- ret=$?
- case "$ret" in
- 0)
- echo "haproxy is running."
- ;;
- 1)
- echo "haproxy dead, but $PIDFILE exists."
- ;;
- *)
- echo "haproxy not running."
- ;;
- esac
- exit $ret
- ;;
- *)
- echo "Usage: /etc/init.d/haproxy {start|stop|reload|restart|status}"
- exit 2
- ;;
- esac
- :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement