shell script to auto restart service

Shell Script to Restart Service if Not Running

Sometimes certain services & processes stop running on our system due to some reason or the other. If it is a long running service that we are waiting to run, this can be very disappointing. Although we take time to investigate into the issue and fix it, it might be better to autorestart a service when it stops. While there are many third-party services like supervisord that offer this feature, you can also do this by writing a simple shell script.


Shell Script to Restart Service if Not Running

For our example, we will create a simple shell script that automatically runs via the crontab every 15 minutes and restarts a specific process if it is not running.

Here is the basic command to check if a process, such as mysql, is running or not.

ps -ef | grep mysql |grep -v grep > /dev/null

The above ps command lists all process that are running. We pass its output to grep command and search for the string ‘mysql’ to see if it contains an entry for MySQL process. We also pass it to grep command once again to exclude the entry of grep command executed before, since that already contains the string ‘mysql’ in it.

If the exit status of above command is not 0, it means that the process is not running. Now the command to start MySQL process is

/etc/init.d/mysql start > /dev/null 

Let us put all this together in a shell script. Create a blank shell script with the following command.

$ vi auto-restart.sh

Add the following lines to it.

#!/bin/bash
ps -ef | grep mysql |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/mysql start > /dev/null 
fi

Save and close the file. In the above script, we first set the execution environment, then run the above mentioned ps command to check if MySQL is running or not. Then we check the exit status of that command, stored in $? variable. If it is not 0, we run the command to start MySQL service.

Make the shell script executable with the following command.

$ chmod 755 auto-restart.sh

You can run the shell script with the following command.

$ ./auto-restart.sh

To schedule the above shell script to run every 15 minutes, run the following command to open crontab.

$ crontab -e

Add the following line to it. Make sure to mention the full path to your shell script (e.g. /home/ubuntu/auto-restart.sh) so that cron process can find it correctly.

*/15 * * * * /home/ubuntu/auto-restart.sh

Save and close the file. That’s it. Now your shell script will automatically run every 15 minutes, checking if MySQL is running or note, and autostart if it has died, crashed or stopped working.

In your shell script, you can add code to check for more processes, to check if they are running or not, and start them, if they have stopped.

Here is an example to check if PHP and Apache, in addition to MySQL, and start them if they are down.

#!/bin/bash
ps -ef | grep mysql |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/mysql start > /dev/null 
fi
ps -ef | grep apache |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/apache2 start > /dev/null
fi
ps -ef | grep php5-fpm |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/php5-fpm start > /dev/null
fi

In this article, we have learnt how to create a shell script to restart service. You can customize it as per your requirement.

Also read:

Shell Script to Read Data from Text File
Awk Split One Column into Multiple Columns
How to Read Variable from File in Shell Script
Shell Script to Print Output in Table
How to Change PHP Version in Ubuntu

2 thoughts on “Shell Script to Restart Service if Not Running

  1. Hello,

    Thank you for the useful tutorial, but I have one comment, and correct me if I’m wrong but your cron schedule would run every minute instead of every 15 minutes

    “*/1 * * * * /home/ubuntu/auto-restart.sh”

    • Thank you for pointing it out. We have updated the tutorial accordingly. It should be
      */15 * * * * /home/ubuntu/auto-restart.sh

Leave a Reply

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