Skip to content

Instantly share code, notes, and snippets.

@kuldipem
Last active June 13, 2020 10:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuldipem/d526a8ea1cf3636e7cbda832aa01b9df to your computer and use it in GitHub Desktop.
Save kuldipem/d526a8ea1cf3636e7cbda832aa01b9df to your computer and use it in GitHub Desktop.
Ubuntu/Linux shell script to run using cron to confirm services status, if not running then send email and start the service
#!/bin/sh
APACHE2_STATUS="$(systemctl is-active apache2.service)"
MYSQL_STATUS="$(systemctl is-active mysql.service)"
NGINX_STATUS="$(systemctl is-active nginx.service)"
DOVECOT_STATUS="$(systemctl is-active dovecot.service)"
START_SCRIPT_DEBUG="true"
FLAG_STATUS="active"
SendEmail(){
if [ $1 = "active" ]
then
echo "$2 is running" | mail -s "$2 Status" kuldipem@gmail.com
else
echo "$2 is not running" | mail -s "$2 Status" kuldipem@gmail.com
fi
}
if [ $APACHE2_STATUS = $FLAG_STATUS ]
then
echo "Apache2 is running"
else
SendEmail $APACHE2_STATUS "Apache2"
/etc/init.d/apache2 start > /dev/null
fi
if [ $MYSQL_STATUS = $FLAG_STATUS ]
then
echo "mysql is running"
else
SendEmail $MYSQL_STATUS "mysql"
/etc/init.d/mysql start > /dev/null
fi
if [ $NGINX_STATUS = $FLAG_STATUS ]
then
echo "nginx is running"
else
SendEmail $NGINX_STATUS "nginx"
/etc/init.d/nginx start > /dev/null
fi
if [ $DOVECOT_STATUS = $FLAG_STATUS ]
then
echo "dovecot is running"
else
SendEmail $DOVECOT_STATUS "dovecot"
/etc/init.d/dovecot start > /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment