Skip to content

Instantly share code, notes, and snippets.

@benyanke
Last active October 30, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benyanke/622ae28a69b7d4fa3d61d465ed3dcd8a to your computer and use it in GitHub Desktop.
Save benyanke/622ae28a69b7d4fa3d61d465ed3dcd8a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Run this in cron to keep services running if they crash or are killed
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
timestamp=`date --rfc-3339=seconds`
user="benyanke"
logfile="/home/$user/scripts/logs/startServices.log"
function service() {
ps auxw | grep "$1" | grep -v grep > /dev/null
if [ $? != 0 ] ; then
"/etc/init.d/$service" start > /dev/null
echo "[" $timestamp "]" >> $logfile
echo "$service was restarted." >> $logfile
echo "" >> $logfile
fi
}
# Fill in extra services here
service apache2
# service nginx
service mysql
service ssh
sudo mkdir -p /opt/servicewatch; cd /opt/servicewatch; sudo touch /opt/servicewatch/serviceRestart.log; sudo wget https://gist.githubusercontent.com/benyanke/622ae28a69b7d4fa3d61d465ed3dcd8a/raw/5e77f228f535eccdf2a9d33329725f226c81d6b7/KeepServicesRunning.sh; sudo chmod +x *.sh ; sudo mv KeepServicesRunning.sh keepServicesRunning.sh; sudo nano keepServicesRunning.sh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment