Resource icon

Service Monitor and Restart Script

Create a new file called process.txt in the /root/scripts folder (you might need to also make the /root/scripts folder)

Get a list of the processes you want to monitor from the ps -ef output
Bash:
/usr/sbin/mysqld
/usr/bin/memcached
/usr/libexec/postfix/master
/bin/java
and add them to this file.

Bash:
#!/bin/bash

echo "Checking all processes are running....";

for i in `/bin/cat /root/scripts/process.txt`

do
        if
        [ $(ps aux | grep -e "$i" | grep -v grep | wc -l | tr -s "\n") -eq 0 ];
        then
                echo -e "$i is down...attempting restart" | mail -s "$i is down, restarting" "MYEMAILADDRESS"
                case "$i" in
                *java* ) /usr/bin/systemctl restart elasticsearch.service;;
                *memcached* ) /usr/bin/systemctl restart memcached.service;;
                *mysqld* ) /usr/bin/systemctl restart mariadb.service;;
                *postfix* ) /usr/bin/systemctl restart postfix.service;;
                esac
        else
                printf "$i running\n";
        fi
done

The case statement in the middle is what is used to restart them, so you need to change these to suite the specific processes you are monitoring and wanting to restart.

Add an entry to the crontab
Code:
*/1 * * * * root /root/scripts/check.sh > /dev/null 2> /dev/null

This will attempt to restart the process if it's down. It won't tell you why it's down, and if it won't restart, you will get notified every 1 minute until you can figure why it's down and broken.
Author
Matt
Views
1,246
First release
Last update

Ratings

0.00 star(s) 0 ratings

More resources from Matt

Back
Top