Simple script to restart services automatically when stopped in Fedora/Redhat


fedora_logoWhen occasionally the services like Apache or MySQL or other rc.d script crashes and stops working, then instead of restarting them manually, it may be more desirable to restart them automatically. A friend of mine asked about such an issue and here is the quick fix bash script fix.

Service auto-restart script

#!/bin/bash

#store the pid of this script into a temp file
echo $$ > /tmp/autorestart_pid

declare -a service_arr

#Load this array with the service names (maybe from file)
service_arr=("httpd" "mysqld" "dhcpd")

#polling interval in seconds
poll_interval=2
n=${#service_arr[*]}

while [ true ]
  do
    for ((i=0; i<n; i++))
      do
        service ${service_arr[i]} status &> /dev/null
        if [ $? -ne 0 ]
          then
            service ${service_arr[i]} start &> /dev/null

            if [ $? -ne 0 ]
              then
                echo "Failed to start ${service_arr[i]}"
            fi
        fi
      done
  #sleep poll_interval time
  sleep $poll_interval
done

One service list is made (httpd etc above code). The inner for loops over each of these services and checks if they are alive by using the service command. Service will return true (0) if the service is alive and false (non 0) otherwise. If the service is dead we simply attempt to restart the service. If starting the service fails, then we will have a false return value in $?. In such a case a message is printed in stdout. This can be later redirected into some file or whatever for notification of this troublesome service.

This for loop is enclosed in the outer while loop which spins infinitely. The poll_interval determines after how many seconds the inner for loop will execute. Keeping no poll interval will take a lot of CPU time, therefore it should be set as per requirement.
Both the stdout and stderr (with &>) are redirected to /dev/null, as we do now want to see the script outputs.

Start it

We need to run this script as root, as starting/stopping a service is privileged operation. You can run this script once you login as root and send it in background.

[phoxis@localhost phoxis]# ./autorestart.sh &

Other way to start it is on each reboot by entering this script in the crontab. To do this do the following as superuser.

[root@localhost phoxis]# crontab -e

Then enter the following line in the editor, save and exit (:wq)

@reboot /path/to/autorestart.sh

Stop it

Note that we have saved the pid ($$) of the script in a file /tmp/autorestart_pid . We can use this pid value with an auxiliary script stop_autorestart.sh, which will read the pid from this file and kill it. This script is also to be run as super user. The stop script is as below.

#!/bin/bash

pid=$(cat /tmp/autorestart_pid)

sudo kill $pid

This simply reads the pid from the file and kills the process with sudo. We can also do su -c or simply run the script as root.

More on this

The above script looks good but i am unsure about how they will perform in a real server system. Here is an application called Monit, and an overview about it.

Quoted from http://www.cyberciti.biz/tips/howto-monitor-and-restart-linux-unix-service.html

monit is a utility for managing and monitoring processes, files, directories and devices on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. For example, monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses to much resources. You may use monit to monitor files, directories and devices for changes, such as timestamps changes, checksum changes or size changes.

Links and References

About these ads

About phoxis

Homo-sapiens
This entry was posted in Computer Science, GNU+Linux+FOSS, Linux / Unix Shell, Tutorials and tagged , , . Bookmark the permalink.

4 Responses to Simple script to restart services automatically when stopped in Fedora/Redhat

  1. I wish I knew this script about a week back. My client’s website went down at random intervals due to a MySQL crash. We could not install Monit either, as we had restrictions on installing third-party tools, even though I’ve had a lot of success with Monit with other servers.

    Only a few days back, we troubleshooted this issue after many hours of observation. Basically, in that server, inode values peaked to the maximum, MySQL was highly inefficient and a few other minor issues. But, I’m going to deploy your script, if I get the approval.

    Thanks for much for sharing and love to see you blogging again after a long gap last year.

    • phoxis says:

      Yes you can use the code. All the posts are under CC-NC-ND and codes under GPLv3. Check out the license page: http://phoxis.org/about/#license. Just have a look at the licenses and you would know what you can do with the code/post.

      Ya, last year it was a long gap, I have recently started releasing short and quick posts without thinking much, instead of a big well constructed post. I hope this plan would save the blog from starving.

      • Thanks phoxis for the permission to use the code under the above mentioned license.

        I never think about how long a post is, when I look at someone’s blog. We never know how useful that’d be for someone like me. :)

  2. Suresh says:

    Very useful Thanks for the code ….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s