// Tutorial //

How To Write a Bash Script To Restart Server Programs

Published on August 30, 2012
Default avatar
By Etel Sverdlov
Developer and author at DigitalOcean.
How To Write a Bash Script To Restart Server Programs

To ensure that the most imperative programs remain online as much as possible (even after a server crash or reboot), one can create a short bash script to check if the program is running, and if it is not, to launch it. By using cron to schedule the script to be executed on a regular basis, we can make sure that program relaunches whenever it goes down.

Bash Script

The first step in this process is to create the script itself. There are a variety of programs such as upstart, supervisor, and monit, that have the capability to start and monitor applications on a virtual private server in a very nuanced way— this bash script will simply provide an on switch.

Below is a sample script that starts apache if it finds it off.

nano launch.sh
#!/bin/sh

ps auxw | grep apache2 | grep -v grep > /dev/null

if [ $? != 0 ]
then
        /etc/init.d/apache2 start > /dev/null
fi

Once you have saved the script, you must give it executable permissions in order to be able to run it:

chmod +x launch.sh

Apache can be replaced with any required application. Should you want to set up the script for a variety of applications, you can create a new script for each one, placing it on its own line in the cron file.

Cron Setup

With the script in hand, we need to set up the schedule on which it will run. The cron utility allows us to schedule at what intervals the script should execute. Start by opening up the cron file:

crontab -e

Cron has a detailed explanation of how the timing system works at the beginning.

Once you know how often you want the script to run, you can write in the corresponding line.

The most often that the script can run in cron is every minute. Should you want to set up such a small increment, you can use this template:

* * * * * ~/launch.sh

Every five minutes would be set up like this:

*/5 * * * * ~/launch.sh

See More

Setting up this simple script will keep the program starting up after it shuts down for any reason. This is convenient as it will ensure that the longest time that a program will be down is for the interval of time that you specified in the cron configuration.

Should you need a program that is even slightly more subtle, you can set up the details of your startup with one of the several server monitoring programs (Supervisor, Upstart, or Monit).

By Etel Sverdlov

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
10 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This one line cron job is how I am doing it :

Autorestart Apache2 if it isn’t running

          • /usr/sbin/service apache2 status > /dev/null || /usr/sbin/service apache2 start

It hasn’t failed yet, even if apache2 crashes . The/dev/null bit means I don’t get emailed every minute with a success message. It “just works”.

Could this script be modified to make java execute a .jar file whenever a Minecraft server crashes?

Instead of this approach:

$ ps auxw | grep mysql | grep -v grep > /dev/null

There are more elegant ways:

pgrep -q mysqld

or this:

pkill -0 mysqld

This is an excellent script. I love your one click wordpress setup but have had it go down so many times. Crossing my fingers this solves it.

I need script to stop the MySQL and take backup incrementally,then automatically start the MySQL at midnight.Thanks!

I’m using tomcat server. tomcat7 service is avalible I want write cronjob for if my tomcat server is resposnding then restart the tomcat server.

I made just like you wrote (script, chmod, cron) and have an error: apachecheck.sh: 9: apachecheck.sh: Syntax error: end of file unexpected (expecting “then”)

Script runs at Ubuntu 14.04.

Would you help me?

OK, found the solution: just check for the existence of the word “not” in the result from

service apache2 status

Here’s the script:

#!/bin/sh

RESTART="/etc/init.d/apache2 restart"
SERVICE="apache2"
LOGFILE="/var/log/uwe-crontab/crons.log"

#check for the word not in the result of status
if service apache2 status | grep -q not
then
        echo "restarting apache at $(date)" >> $LOGFILE
        $RESTART >> $LOGFILE
else
        echo "apache is running at $(date)" >> $LOGFILE
fi

and the entry in root’s crontab (crontab -e) is

* * * * * /bin/bash /etc/apache2/relaunch.sh

This will run the script every minute.

Using this at http://www.fairtattoo.com

Some problem here too.

My script works just fine. It is in /etc/apache2/relaunch.sh, file permissions are

-rwxr-xr-x   1 root root   372 Feb 21 08:01 relaunch.sh*

here’s the script

#!/bin/sh
echo "running apache2 relaunch at $(date)" >> /var/log/uwe-crontab/crons.log
ps auxw | grep apache2 | grep -v grep > /dev/null

if [ $? != 0 ]
then
        /etc/init.d/apache2 restart >> /var/log/uwe-crontab/crons.log
        echo "apache relaunched at $(date)" >> /var/log/uwe-crontab/crons.log
else 
        echo "apache is running"  >> /var/log/uwe-crontab/crons.log
fi

I try to run it from root’s cron. Here’s the cron entry.

* * * * * sh /etc/apache2/relaunch.sh

Strangely, the script fires every minute and writes an entry into my log file without a problem, but won’t work correctly when it is run from Cron. Is it some problem with environment variable? Maybe I need to set that in the script too?

If I stop Apache, the script still writes the the log file but doesn’t restart Apache.

Sorry but it does not work… It doesn’t restart my programs…