Reload HAProxy Without connection loss

Bellow is a quick and dirty script to reload haproxy without dropping connections, just fill the correct values at lines 2,3,4 and 5 and you a probably good2go
 
The script is really simple, after you plug the values for the variables the execution can be sumarized in this steps:
1) Create the PID directory in case it doesn’t exists
2) Checks to see if it found a valid configuration file
3a) if the configuration is valid look for another haproxy running and send it a signal to stop, while starting a new haproxy
3b) abort the restart and print the errors found on the config file
 

#!/bin/bash
CFG_FILE="/etc/haproxy/haproxy.cfg"
HAPROXYBIN=$(which haproxy)
PIDDIR="/var/run/haproxy/"
PIDFILE=${PIDDIR}"haproxy.pid"

[ ! -d ${PIDDIR} ] && mkdir -p ${PIDDIR}


# Check if the configuration is valid
${HAPROXYBIN} -c -f ${CFG_FILE} | tail -1 | grep "Configuration file is valid" -q
VALID=$?
if [[ ${VALID} == 0 ]];then
    echo "Found a Valid Configuration file, reloading HAProxy"
    ${HAPROXYBIN} -f ${CFG_FILE} -p ${PIDFILE} -sf $(cat ${PIDFILE})
else
    echo "Invalid Configuration file"
    ${HAPROXYBIN} -c -f ${CFG_FILE}
fi