$ sudo mkdir /etc/scripts
$ sudo vi /etc/scripts/checkService.sh

#!/bin/bash
#
#---------[SCRIPT CHECK SERVICE]---------#
# Author by Aditya Maulana #
# 2018-06-06 17:00 #
#----------------------------------------#
#
TITLE="AG!NK - 103.aaa.bbb.ccc"
HOST_NAME=`hostname`
SERVICE_NGINX=nginx.service
SERVICE_FPM=php7.2-fpm
SERVICE_CSF=csf.service
SERVICE_LFD=lfd.service
SERVICE_MEM=memcached.service
SERVICE_MYSQL=mysql.service
SERVICE_POSTFIX=postfix.service
SERVICE_DOVECOT=dovecot.service
SERVICE_OPENDKIM=opendkim.service
SERVICE_OPENDMARC=opendmarc.service
SERVICE_SPAM=spamassassin.service
SERVICE_AMAVIS=amavisd.service
SERVICE_TOMCAT=tomcat.service
[email protected]
ExecutedBy=`whoami`
#
# WHATSAPP
#
USERID="124567890"
KEY="7450960XX:AAHgva-2O7VjcwAyxvoWG0kp2V01DgrD6X8"
URL=https://api.telegram.org/bot$KEY/sendMessage
TIMEOUT=10
#
# TOMCAT
#
if [ `systemctl is-active ${SERVICE_TOMCAT}` != "active" ]
then
echo "$SERVICE_TOMCAT is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_TOMCAT}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_TOMCAT} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_TOMCAT} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_TOMCAT}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# NGINX
#
if [ `systemctl is-active ${SERVICE_NGINX}` != "active" ]
then
echo "$SERVICE_NGINX is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_NGINX}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_NGINX} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_NGINX} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_NGINX}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# PHP - FPM
#
if [ `systemctl is-active ${SERVICE_FPM}` != "active" ]
then
echo "$SERVICE_FPM is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_FPM}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_FPM} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_FPM} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_FPM}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# CSF
#
if [ `systemctl is-active ${SERVICE_CSF}` != "active" ]
then
echo "$SERVICE_CSF is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_CSF}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_CSF} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_CSF} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_CSF}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# LFD
#
if [ `systemctl is-active ${SERVICE_LFD}` != "active" ]
then
echo "$SERVICE_LFD is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_LFD}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_LFD} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_LFD} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_LFD}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# MEMCACHE
#
#if [ `systemctl is-active ${SERVICE_MEM}` != "active" ]
#then
# echo "$SERVICE_MEM is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_MEM}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_MEM} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_MEM} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_MEM}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# MYSQL
#
if [ `systemctl is-active ${SERVICE_MYSQL}` != "active" ]
then
echo "$SERVICE_MYSQL is not running on $HOST_NAME restarting it"
systemctl restart ${SERVICE_MYSQL}
Exit_Code=`echo $?`
if [ ${Exit_Code} = "0" ]; then
# Send email notification
#mail -s "${SERVICE_MYSQL} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_MYSQL} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# Send telegram notification
TEXT="*[${TITLE}]*%0A%0A *${SERVICE_MYSQL}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
exit 0
fi
fi
#
# POSTFIX
#
#if [ `systemctl is-active ${SERVICE_POSTFIX}` != "active" ]
#then
# echo "$SERVICE_POSTFIX is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_POSTFIX}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_POSTFIX} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_POSTFIX} was restarted on ${HOST_NAME} run by ${ExecutedBy}
"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_POSTFIX}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# DOVECOT
#
#if [ `systemctl is-active ${SERVICE_DOVECOT}` != "active" ]
#then
# echo "$SERVICE_DOVECOT is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_DOVECOT}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_DOVECOT} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_DOVECOT} was restarted on ${HOST_NAME} run by ${ExecutedBy}
"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_DOVECOT}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# OPENDKIM
#
#if [ `systemctl is-active ${SERVICE_OPENDKIM}` != "active" ]
#then
# echo "$SERVICE_OPENDKIM is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_OPENDKIM}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_OPENDKIM} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_OPENDKIM} was restarted on ${HOST_NAME} run by ${ExecutedB
y}"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_OPENDKIM}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# OPENDMARC
#
#if [ `systemctl is-active ${SERVICE_OPENDMARC}` != "active" ]
#then
# echo "$SERVICE_OPENDMARC is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_OPENDMARC}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_OPENDMARC} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_OPENDMARC} was restarted on ${HOST_NAME} run by ${Execute
dBy}"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_OPENDMARC}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# SPAM
#
#if [ `systemctl is-active ${SERVICE_SPAM}` != "active" ]
#then
# echo "$SERVICE_SPAM is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_SPAM}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_SPAM} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_SPAM} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_SPAM}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
#
# AMAVIS
#
#if [ `systemctl is-active ${SERVICE_AMAVIS}` != "active" ]
#then
# echo "$SERVICE_AMAVIS is not running on $HOST_NAME restarting it"
# systemctl restart ${SERVICE_AMAVIS}
# Exit_Code=`echo $?`
# if [ ${Exit_Code} = "0" ]; then
# # Send email notification
# mail -s "${SERVICE_AMAVIS} was restarted on ${HOST_NAME}" ${EMAIL_TO} <<< "${SERVICE_AMAVIS} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
# # Send telegram notification
# TEXT="*[${TITLE}]*%0A%0A *${SERVICE_AMAVIS}* was restarted on ${HOST_NAME} run by ${ExecutedBy} %0A%0A---%0AAuthor by *Aditya Maulana*"
# curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&parse_mode=Markdown&text=$TEXT" $URL > /dev/null
# exit 0
# fi
#fi
exit 0

$ sudo chmod +x /etc/scripts/checkService.sh
$ sudo crontab -e

#--- Check Service ---#
*/5 * * * * /etc/scripts/checkService.sh > /dev/null 2>&1