Simple script to keep key process running

1 Like 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

This guy likes taking photos, hunting cheap tours, exploring the unique culture of every cities of Vietnam. Contact: [email protected]