What is happening with HaaS?

As far as HaaS could sometimes stop working, I created small script which checks whether HaaS (haas-proxy) is working, and in case not, restart it. This script could be run by cron (with option -s and maybe -l)… I’d like to publish it for anyone use… Feel free to use/modify it as you like :wink:

checkHaaS.sh
# checkHaaS.sh        (c) jada4p                  v2 20220222

# check whether HaaS is running, restart it when not

SILENT=NO
LOG=NO

LOGFILE=`awk -v FS="\'" '/log / { print $2 }' /etc/config/haas`
#LEVEL=`awk -v FS="\'" '/log_level / { print $2 }' /etc/config/haas`
#TOKEN=`awk -v FS="\'" '/token / { print $2 }' /etc/config/haas`
#echo LOGFILE=$LOGFILE
#echo LEVEL=$LEVEL
#echo TOKEN=$TOKEN

for i
do
case $i in
-i) # -----------------------------------help
    echo "checkHaaS.sh check whether HaaS is running, restart it when not"
    echo "checkHaaS.sh -s silent - no runtime comments"
    echo "checkHaaS.sh -l log actions"
    exit
    ;;
-s) # -----------------------------------silent
    SILENT=YES
    ;;
-l) # -----------------------------------log
    LOG=YES
    if [ "$LOGFILE" = "" ]
      then LOGFILE="/var/log/messages"
    fi
    ;;
*)  echo "invalid parameter(s), aborted"
    /root/checkHaaS.sh -i
    exit
    ;;
esac
done

# ---------------------------------------------------------check proxy pid
if [ "$SILENT" = "NO" ]
  then echo "Checking if exist haas-proxy PID"
fi
if [ ! -s /var/run/haas-proxy.pid ]
  then if [ "$SILENT" = "NO" ]
         then echo "HaaS is not running, no haas-proxy PID, starting it"
       fi
       /etc/init.d/haas-proxy reload
       /etc/init.d/haas-proxy start
       if [ "$LOG" = "YES" ]
         then DATE=`date +"%Y-%m-%d %T %Z"`
              MSG="checkHaaS: no PID found, HaaS restarted"
              echo "$DATE $MSG" >> $LOGFILE
       fi
  else if [ "$SILENT" = "NO" ]
         then echo "HaaS is running, haas-proxy PID found"
       fi
fi
# ---------------------------------------------------------check process
if [ "$SILENT" = "NO" ]
  then echo "Checking if there is HaaS process"
fi
sleep 10         # for some reason PID file is not found when testing
                 # immediately after restart
if [ $(ps `cat /var/run/haas-proxy.pid` | grep token | wc -l) -ne 1 ]
  then if [ "$SILENT" = "NO" ]
         then echo "HaaS is not running, starting it"
       fi
       /etc/init.d/haas-proxy reload
       /etc/init.d/haas-proxy start
       if [ "$LOG" = "YES" ]
         then DATE=`date +"%Y-%m-%d %T %Z"`
              MSG="checkHaaS: no process found, HaaS restarted"
              echo "$DATE $MSG" >> $LOGFILE
       fi
  else if [ "$SILENT" = "NO" ]
         then echo "HaaS is running"
       fi
fi

Note there are some commented lines on beginning of script, they were used for debugging and left for possible future use. The script is supposed to reside in /root directory.

1 Like