Simple script to keep key process running

84 Likes Comment

If you’re running a VPS to host your website blog, how could you keep your service running

Yes, It’s simple. You need to monitor the key (important) services, processes of your application running.

Here’s my tested solution in a bash shell script.

#!/bin/bash

if pgrep -x "SERVICENAME" > /dev/null
then
    echo "Running NORMALLY"
else
    //command to start the service
    start "SERVICENAME"
fi

This script will check the service / process is running or not, then start your service automatically.

For an example , here is the script to check MySQL service

#!/bin/bash

if pgrep -x "mysqld" > /dev/null
then
    echo "MYSQL IS Running NORMALLY"
else
    //command to start the service
    sudo systemctl start mysqld
fi

Save the above code as: check-myservice.sh

Then grant execute right the file: chmod +x check-myservice.sh

Add this script to the crontab:

*/5 * * * * /bin/sh /root/mytabs/check-myservice.sh

Hope this helps.

You might like

About the Author: Toc Xoan

Leave a Reply

Your email address will not be published. Required fields are marked *