Skip to content

Instantly share code, notes, and snippets.

@kuldipem
Last active Jun 13, 2020
Embed
What would you like to do?
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