Script automatically check nginx service and restart

Share this post to your friends !

WriteBash - This article will introduce you to a script that automatically check the nginx service and restart if the nginx service failed.

Everyone knows nginx is a popular web server with incredible power.

What all system administrators care about is that if the nginx service is failed in the middle of the night. So what to do?

You will need an automated script to do something.

Script automatically check nginx service and restart

I wrote a simple script during my work.

About the script

First, I have to say that this script I wrote for CentOS 6. You can change it a bit for your system, it’s quite easy.

script-automatically-check-nginx-service-and-restart Script automatically check nginx service and restart
Script automatically check nginx service and restart.

Most of the time I work with nginx, I see there are times when the nginx service is not working. Cause, depending on circumstances, the server may be overloaded RAM.

My script does the following:

  • Get the current status of nginx service.
  • Check if the status is not running, immediately restart the service. Send alert email to admin.
  • If restart the service failed and status services is nginx dead but subsys locked then delete the subsys locked file and restart nginx. Send alert email to admin.

Note the change in the script

In the script, before you use it, you need to change some information in it.

  1. change to the email address you use to send email.
  2. change to your first administrator’s email address.
  3. change to your second administrator’s email address. If there is only one administrator, just delete this email address in the script.

And one more thing, the script uses mail command to send alert emails. Please use the command below to check if it exists or not.

# mail --help

If not, install the mailx package.

# yum install mailx -y

Download and use script

First, use the following command to download the script to the server, put the script in the /opt directory.

# wget -O /opt/

Then you assign the executable permissions to the script.

# chmod 0700 /opt/

You already have the script, now how to make it run automatically, we must use crontab.

Use the command below to add crontab.
# crontab -l | { cat; echo "# Check nginx service every 1 minute & auto restart service if it's failed"; } | crontab -
# crontab -l | { cat; echo "*/1 * * * * /opt/"; } | crontab -


So you have a simple tool to track nginx service. If at some point, it doesn’t work, you don’t have to worry.

The script will automatically do its job, restart the nginx service if checking that status is not running. It is very simple but very useful in your work.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.