#!/bin/bash
### Pulseway Service Check ###
### Log file location: /var/log/pulseway.log
### Add to crontab for hourly checks at half-past the hour, with no console output:
### 30 * * * * /root/pulseway-service-check.sh > /dev/null 2>&1
### This way, crontab doesn't try to email the result of the job
# Create $variable with the current service status
STATUS=`systemctl show -p SubState --value pulseway`
# For older versions of systemd, skip the "--value"
#STATUS=`systemctl show -p SubState pulseway`
# Create $variable for the log file location
LOG=/var/log/pulseway.log
# Echo $variable value for debugging
# Leave commented out for production
#echo $STATUS
# If status is not "running", then echo message, then restart the service
# If status is "running", then just log it and exit
#if [ "$STATUS" != "SubState=running" ]; then
if [ "$STATUS" != "running" ]; then
echo "Pulseway Service is not Running! $(date)" | tee -a $LOG 2>&1
echo "Restarting Pulseway service" | tee -a $LOG 2>&1
systemctl restart pulseway | tee -a $LOG 2>&1
echo "Current Pulseway service status: " | tee -a $LOG 2>&1
systemctl status pulseway | tee -a $LOG 2>&1
echo $STATUS | tee -a $LOG 2>&1
else
echo "Pulseway Service is currently Running! $(date)" | tee -a $LOG 2>&1
echo $STATUS | tee -a $LOG 2>&1
fi