🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Debian8 ## apt-get 安装的传统启动文件 ``` [root@blog ~]# cat /etc/init.d/haproxy #!/bin/sh ### 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 CONFIG=/etc/haproxy/haproxy.cfg HAPROXY=/usr/sbin/haproxy RUNDIR=/run/haproxy EXTRAOPTS= test -x $HAPROXY || exit 0 if [ -e /etc/default/haproxy ]; then . /etc/default/haproxy fi test -f "$CONFIG" || exit 0 [ -f /etc/default/rcS ] && . /etc/default/rcS . /lib/lsb/init-functions check_haproxy_config() { $HAPROXY -c -f "$CONFIG" >/dev/null if [ $? -eq 1 ]; then log_end_msg 1 exit 1 fi } haproxy_start() { [ -d "$RUNDIR" ] || mkdir "$RUNDIR" chown haproxy:haproxy "$RUNDIR" chmod 2775 "$RUNDIR" check_haproxy_config start-stop-daemon --quiet --oknodo --start --pidfile "$PIDFILE" \ --exec $HAPROXY -- -f "$CONFIG" -D -p "$PIDFILE" \ $EXTRAOPTS || return 2 return 0 } haproxy_stop() { if [ ! -f $PIDFILE ] ; then # This is a success according to LSB return 0 fi ret=0 for pid in $(cat $PIDFILE); do start-stop-daemon --quiet --oknodo --stop \ --retry 5 --pid $pid --exec $HAPROXY || ret=$? done [ $ret -eq 0 ] && rm -f $PIDFILE return $ret } haproxy_reload() { check_haproxy_config $HAPROXY -f "$CONFIG" -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 : ``` ## apt-get 安装后systemd的启动文件 ``` [root@blog ~]# cat cat /lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer Documentation=man:haproxy(1) Documentation=file:/usr/share/doc/haproxy/configuration.txt.gz After=network.target syslog.service Wants=syslog.service [Service] Environment=CONFIG=/etc/haproxy/haproxy.cfg EnvironmentFile=-/etc/default/haproxy ExecStartPre=/usr/sbin/haproxy -f ${CONFIG} -c -q ExecStart=/usr/sbin/haproxy-systemd-wrapper -f ${CONFIG} -p /run/haproxy.pid $EXTRAOPTS ExecReload=/usr/sbin/haproxy -c -f ${CONFIG} ExecReload=/bin/kill -USR2 $MAINPID KillMode=mixed Restart=always [Install] WantedBy=multi-user.target ``` ## 简单的启动管理 ``` #!/bin/bash # # chkconfig: 35 85 15 # description: haproxy server control file set -e PROGDIR=/usr/local/haproxy PROGNAME=haproxy DAEMON=$PROGDIR/sbin/$PROGNAME CONFIG=$PROGDIR/conf/$PROGNAME.conf PIDFILE=$PROGDIR/run/$PROGNAME.pid DESC="HAProxy daemon" SCRIPTNAME=/etc/init.d/$PROGNAME E_NO_PERM=65 # Gracefully exit if the package has been removed. test -x $DAEMON || exit $E_NO_PERM start() { echo -n "Starting $DESC:$PROGNAME" $DAEMON -f $CONFIG echo "." } stop() { echo -n "Stopping $DESC:$PROGNAME" haproxy_pid=$(cat $PIDFILE) kill $haproxy_pid echo "." } restart() { echo -n "Restarting $DESC: $PROGNAME" $DAEMON -f $CONFIG -p $PIDFILE -st $(cat $PIDFILE) echo "." } case "$1" in start) start ;; stop) stop ;; restart) restart ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2 exit 1 esac ```