How To Use a Bash Script To Restart Linux Server Services

Use a script to restart critical Linux services such as the web or database server. Restart nginx, apache2, MySQL or PHP-fpm automatically.

If you manage your own WordPress web server then you have undoubtedly come across many situations where your web apache, nginx, MySQL or PHP-fpm services have stopped.

Sometimes the reason is unknown and things just crash from time to time.

Using the Linux crontab service we can write a simple bash script to test to see if these services have stopped and restart them.

You can use whatever editor you are comfortable with.

Here’s the command line code to create the file in nano:

sudo nano /opt/

Here is the bash script.

[pastacode lang=”bash” manual=”%23!%2Fbin%2Fbash%0A%0Aservice%20mysql%20status%20%7C%20grep%20’active%20(running)’%20%3E%20%2Fdev%2Fnull%202%3E%261%0A%0Aif%20%5B%20%24%3F%20!%3D%200%20%5D%0Athen%0A%20%20%20%20%20%20%20%20sudo%20service%20mysql%20restart%20%3E%20%2Fdev%2Fnull%0Afi%0A%0Aservice%20nginx%20status%20%7C%20grep%20’active%20(running)’%20%3E%20%2Fdev%2Fnull%202%3E%261%0A%0Aif%20%5B%20%24%3F%20!%3D%200%20%5D%0Athen%0A%20%20%20%20%20%20%20%20sudo%20service%20nginx%20restart%20%3E%20%2Fdev%2Fnull%0Afi%0A%0Aservice%20php7.2-fpm%20status%20%7C%20grep%20’active%20(running)’%20%3E%20%2Fdev%2Fnull%202%3E%261%0A%0Aif%20%5B%20%24%3F%20!%3D%200%20%5D%0Athen%0A%20%20%20%20%20%20%20%20sudo%20service%20php7.2-fpm%20restart%20%3E%20%2Fdev%2Fnull%0Afi%0A” message=”” highlight=”” provider=”manual”/]

Change the service names to the ones you are running, e.g. “apache2” or whatever PHP version you are running.

The script uses the service <name> status command to output the status of a particular service such as mysql.

We then run this through grep looking for the phrase “active (running)”.

If this is not found, we ask the system to restart the service.

Save the file to /opt/

Then ensure that it is runnable from the command line using:

sudo chmod +x /opt/

Scheduling Service Restarts Using Crontab

It would be a pain to have to SSH into our server every time a service crashes to run the script.

Instead we can call the script directly from a crontab service and have it running as frequently as we need it to.

Edit your root crontab list using:

sudo crontab -e

It’s important to use the root crontab using the command above and not to edit your own user profile crontab, otherwise, it will not work properly.

Add the following line to the bottom of the root crontab list:

*/1 * * * * /opt/ > /dev/null 2>

This will run the script every minute but you can change that for whatever works for your server.

Now if a critical service crashes, the server will attempt to restart it.

Happy days.

Looking to level up your WordPress development skills?

WordPress Plugin Development book

I self-taught myself WordPress development, but this book took it to another level.

Each chapter builds upon the next, but you can also use it to jump into specific areas like WP Cron, WP REST endpoints, building a custom plugin repository etc.

If you’re like me and like having reference books at hand, this is the one that sits on my desk all the time.

Was this article helpful?

2 thoughts on “How To Use a Bash Script To Restart Linux Server Services”

Comments are closed.


Wil is a dad, WordPress consultant, WordPress developer, business coach and mentor. He co-organizes the WordPress Sydney meetup group and has been on the organising committee for WordCamp Sydney since 2014. He speaks at many special events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.