title: HAproxy process has been killed unexpectlly when HAproxy service wss reloaded by sysv init script in CentOS7.2
body: <p>I installed the HAproxy(1.5.14-3.el7) from CentOS7.2 repository.
  When I reload HAproxy service with a wrong haproxy.cfg,
  the return code of reload is incorrect.</p>
  
  <p>About HAproxy,OS,systemd information pls see below:</p>
  
  <pre><code>[root@unknown ~]# rpm -qa | egrep haproxy
  haproxy-1.5.14-3.el7.x86_64
  [root@unknown ~]#
  
  [root@unknown ~]# cat /etc/redhat-release
  CentOS Linux release 7.2.1511 (Core)
  [root@unknown ~]#
  
  [root@unknown ~]# rpm -qa | egrep systemd
  systemd-libs-219-19.el7.x86_64
  systemd-219-19.el7.x86_64
  systemd-sysv-219-19.el7.x86_64
  [root@unknown ~]#
  </code></pre>
  
  <p>The return code of reload is incorrect.</p>
  
  <pre><code>[root@unknown ~]# service haproxy status
  Redirecting to /bin/systemctl status  haproxy.service
  ●haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled;     vendor preset: disabled)
     Active: active (running) since Tue 2016-06-07 11:24:41 UTC; 4s ago
  [root@unknown ~]#
  [root@unknown ~]#
  [root@unknown ~]# more /etc/haproxy/haproxy.cfg
  XXXX                                                 **--&gt; I added an incorrect keyword into haproxy.cfg**
  Global
  ....
  [root@unknown ~]#
  [root@unknown ~]#
  [root@unknown ~]# service haproxy reload
  Redirecting to /bin/systemctl reload  haproxy.service
  [root@unknown ~]#
  [root@unknown ~]# echo $?
  0                                                   **--&gt; It was sucessful !!!**
  
  [root@unknown ~]#
  [root@unknown ~]# service haproxy status
  Redirecting to /bin/systemctl status  haproxy.service
  ● haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
     Active: active (running) since Tue 2016-06-07 11:24:41 UTC; 21s ago
    Process: 16507 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS)
       Main PID: 16464 (haproxy-systemd)
     CGroup: /system.slice/haproxy.service
             tq16464 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
             tq16465 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
             mq16466 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
  
  Jun 07 11:24:41 unknown systemd[1]: Started HAProxy Load Balancer.
  Jun 07 11:24:41 unknown systemd[1]: Starting HAProxy Load Balancer...
  Jun 07 11:24:41 unknown haproxy-systemd-wrapper[16464]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy...id -Ds
  Jun 07 11:24:57 unknown systemd[1]: Reloaded HAProxy Load Balancer.
  Jun 07 11:24:57 unknown haproxy-systemd-wrapper[16464]: haproxy-systemd-wrapper: re-executing
  Jun 07 11:24:57 unknown haproxy-systemd-wrapper[16464]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy... 16466
  Jun 07 11:24:57 unknown haproxy-systemd-wrapper[16464]: [ALERT] 158/112457 (16508) : parsing [/etc/haproxy/haproxy.cfg:9]: unknown k...ction.  **--&gt; In fact, it was wrong**
  Jun 07 11:24:57 unknown haproxy-systemd-wrapper[16464]: [ALERT] 158/112457 (16508) : Error(s) found in configuration file : /etc/hap...xy.cfg
  Jun 07 11:24:57 unknown haproxy-systemd-wrapper[16464]: [ALERT] 158/112457 (16508) : Fatal errors found in configuration.
  </code></pre>
  
  <p>So I descided to use the sysv init.d script to start/reload/stop HAproxy service.</p>
  
  <p>sysv init.d script:</p>
  
  <pre><code>[root@unknown ~]# cat /etc/init.d/haproxy
  #!/bin/sh
  #
  # chkconfig: - 85 15
  # description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
  #              for high availability environments.
  # processname: haproxy
  # config: /etc/haproxy/haproxy.cfg
  # pidfile: /var/run/haproxy.pid
  
  # Script Author: Simon Matter &lt;simon.matter@invoca.ch&gt;
  # Version: 2004060600
  
  ### BEGIN INIT INFO
  # Provides: HA-Proxy
  # Required-Start: $network $syslog sshd
  # Required-Stop:
  # Default-Start: 3 4 5
  # Default-Stop: 0 1 2 6
  # Short-Description: HAProxy
  ### END INIT INFO
  
  # Source function library.
  if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
  elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
  else
    exit 0
  fi
  
  # Source networking configuration.
  . /etc/sysconfig/network
  
  # Check that networking is up.
  #[ ${NETWORKING} = "no" ] &amp;&amp; exit 0
  
  # This is our service name
  BASENAME=`basename $0`
  if [ -L $0 ]; then
    BASENAME=`find $0 -name $BASENAME -printf %l`
    BASENAME=`basename $BASENAME`
  fi
  
  [ -f /etc/$BASENAME/$BASENAME.cfg ] || exit 1
  
  RETVAL=0
  
  start() {
    /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
    if [ $? -ne 0 ]; then
      echo "Errors found in configuration file, check it with '$BASENAME check'."
      return 1
    fi
  
    echo -n "Starting $BASENAME: "
    daemon /usr/sbin/$BASENAME -D -f /etc/$BASENAME/$BASENAME.cfg -p /var/run/$BASENAME.pid
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] &amp;&amp; touch /var/lock/subsys/$BASENAME
    return $RETVAL
  }
  
  stop() {
    killproc $BASENAME -USR1
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$BASENAME
    [ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/run/$BASENAME.pid
    return $RETVAL
  }
  
  restart() {
    /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
    if [ $? -ne 0 ]; then
      echo "Errors found in configuration file, check it with '$BASENAME check'."
      return 1
    fi
    stop
    start
  }
  
  reload() {
    /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
    if [ $? -ne 0 ]; then
      echo "Errors found in configuration file, check it with '$BASENAME check'."
      return 1
    fi
    /usr/sbin/$BASENAME -D -f /etc/$BASENAME/$BASENAME.cfg -p /var/run/$BASENAME.pid -sf $(cat /var/run/$BASENAME.pid)
  }
  
  check() {
    /usr/sbin/$BASENAME -c -q -V -f /etc/$BASENAME/$BASENAME.cfg
  }
  
  rhstatus() {
    status $BASENAME
  }
  
      condrestart() {
    [ -e /var/lock/subsys/$BASENAME ] &amp;&amp; restart || :
  }
  
  # See how we were called.
  case "$1" in
    start)
      start
      ;;
    stop)
      stop
      ;;
    restart)
      restart
      ;;
    reload)
      reload
      ;;
    condrestart)
      condrestart
      ;;
    status)
      rhstatus
      ;;
    check)
      check
      ;;
    *)
      echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
      exit 1
  esac
  
  exit $?
  </code></pre>
  
  <p>When I reloaded HAproxy serivce with the correct haproxy.cfg, 
  the command(service haproxy reload) returned 0,
  but HAproxy's status became failed.</p>
  
  <pre><code>[root@unknown ~]# service haproxy status
  ● haproxy.service - LSB: HAProxy
     Loaded: loaded (/etc/rc.d/init.d/haproxy)
     Active: active (running) since Tue 2016-06-07 11:33:22 UTC; 1h 14min ago
       Docs: man:systemd-sysv-generator(8)
    Process: 16636 ExecStart=/etc/rc.d/init.d/haproxy start (code=exited, status=0/SUCCESS)
   Main PID: 16641 (haproxy)
     CGroup: /system.slice/haproxy.service
             mq16641 /usr/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
  
  Jun 07 11:33:22 unknown systemd[1]: Starting LSB: HAProxy...
  Jun 07 11:33:22 unknown haproxy[16636]: Starting haproxy: [  OK  ]
  Jun 07 11:33:22 unknown systemd[1]: Started LSB: HAProxy.
  [root@unknown ~]#
  [root@unknown ~]# service haproxy reload
  Reloading haproxy configuration (via systemctl):           [  OK  ]
  [root@unknown ~]# echo $?
  0                                                      **--&gt; It was sucessful !!!**
  [root@unknown ~]#
  [root@unknown ~]# service haproxy status
  ● haproxy.service - LSB: HAProxy
     Loaded: loaded (/etc/rc.d/init.d/haproxy)
     Active: failed (Result: signal) since Tue 2016-06-07 12:48:01 UTC; 1s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 16869 ExecStop=/etc/rc.d/init.d/haproxy stop (code=exited, status=0/SUCCESS)
    Process: 16863 ExecReload=/etc/rc.d/init.d/haproxy reload (code=exited, status=0/SUCCESS)
    Process: 16636 ExecStart=/etc/rc.d/init.d/haproxy start (code=exited, status=0/SUCCESS)
   Main PID: 16868 (code=killed, signal=KILL)
  
  Jun 07 11:33:22 unknown systemd[1]: Starting LSB: HAProxy...
  Jun 07 11:33:22 unknown haproxy[16636]: Starting haproxy: [  OK  ]
  Jun 07 11:33:22 unknown systemd[1]: Started LSB: HAProxy.
  Jun 07 12:48:00 unknown systemd[1]: Reloaded LSB: HAProxy.
  Jun 07 12:48:00 unknown systemd[1]: haproxy.service: main process exited, code=killed, status=9/KILL **--&gt; It was killed ,but I don't know which process killed it, Cgroup ?**
  Jun 07 12:48:01 unknown haproxy[16869]: [FAILED]
  Jun 07 12:48:01 unknown systemd[1]: Unit haproxy.service entered failed state.
  Jun 07 12:48:01 unknown systemd[1]: haproxy.service failed.
  [root@unknown ~]#
  </code></pre>
  
  <p>I used a newer systemd to get the detailed logs</p>
  
  <pre><code>Jun 07 13:02:59 elb systemd[1]: Starting LSB: HAProxy...
  Jun 07 13:02:59 elb systemd[7010]: Executing: /etc/rc.d/init.d/haproxy start
  Jun 07 13:02:59 elb haproxy[7010]: Starting haproxy: [  OK  ]
  Jun 07 13:02:59 elb systemd[1]: Child 7010 belongs to haproxy.service
  Jun 07 13:02:59 elb systemd[1]: haproxy.service: control process exited, code=exited status=0
  Jun 07 13:02:59 elb systemd[1]: haproxy.service got final SIGCHLD for state start
  Jun 07 13:02:59 elb systemd[1]: Main PID loaded: 7015
  Jun 07 13:02:59 elb systemd[1]: haproxy.service changed start -&gt; running
  Jun 07 13:02:59 elb systemd[1]: Job haproxy.service/start finished, result=done
  Jun 07 13:02:59 elb systemd[1]: Started LSB: HAProxy.          **--&gt; start HAproxy successfully **
  
  
  Jun 07 13:03:27 elb systemd[1]: Trying to enqueue job haproxy.service/reload/replace
  Jun 07 13:03:27 elb systemd[1]: Installed new job haproxy.service/reload as 9504
  Jun 07 13:03:27 elb systemd[1]: Enqueued job haproxy.service/reload as 9504
  
  
  Jun 07 13:03:27 elb systemd[1]: About to execute: /etc/rc.d/init.d/haproxy reload
  Jun 07 13:03:27 elb systemd[1]: Forked /etc/rc.d/init.d/haproxy as 7060
  Jun 07 13:03:27 elb systemd[1]: haproxy.service changed running -&gt; reload
  Jun 07 13:03:27 elb systemd[7060]: Executing: /etc/rc.d/init.d/haproxy reload
  Jun 07 13:03:27 elb systemd[1]: Child 7015 belongs to haproxy.service
  Jun 07 13:03:27 elb systemd[1]: Main PID changing: 7015 -&gt; 7065
  Jun 07 13:03:27 elb systemd[1]: Child 7060 belongs to haproxy.service
  Jun 07 13:03:27 elb systemd[1]: haproxy.service: control process exited, code=exited status=0
  Jun 07 13:03:27 elb systemd[1]: haproxy.service got final SIGCHLD for state reload
  Jun 07 13:03:27 elb systemd[1]: haproxy.service changed reload -&gt; running
  Jun 07 13:03:27 elb systemd[1]: Job haproxy.service/reload finished, result=done
  Jun 07 13:03:27 elb systemd[1]: Reloaded LSB: HAProxy.   **--&gt; successful to reload HAproxy**
  
  Jun 07 13:03:27 elb systemd[1]: Child 7065 belongs to haproxy.service
  Jun 07 13:03:27 elb systemd[1]: haproxy.service: main process exited, code=killed, status=9/KILL **--&gt; process 7065 has been killed unexpectly**
  Jun 07 13:03:27 elb systemd[1]: haproxy.service changed running -&gt; failed
  Jun 07 13:03:27 elb systemd[1]: Unit haproxy.service entered failed state.
  Jun 07 13:03:27 elb systemd[1]: haproxy.service failed.
  Jun 07 13:03:27 elb systemd[1]: haproxy.service: cgroup is empty **--&gt;Did cgroup killed process 7065? Is it a bug of systemd? **
  </code></pre>
  
  <p>In CentOS7.1, I use the sysv init script (pls see above) to reload haproxy ,and  'service haproxy reload' command could return correct result.</p>
  
  <p>I don't know what is wrong in CentOS7.2. I just want to get following results of reloading HAproxy.</p>
  
  <ul>
  <li>When haproxy.cfg file is incorrect, 'service haproxy reload' command returns 1</li>
  <li>When haproxy.cfg file is correct, 'service haproxy reload' command returns 0</li>
  </ul>
  
  <p>Can anyone help me ? Thanks</p>