Linux

Create a Linux Service Monitoring Script

Create a Linux Service Monitoring Script. How to create a simple Linux Bash shell script to monitor Nginx and restart the service when needed

You may have mission-critical workloads running on Linux Server such as Nginx that you want to monitor to ensure the service is healthy. Using a Bash script, we can easily create a piece of script code that can monitor a Linux service and then use CRON to run the script on a specified interval to monitor the status of a service. When I googled around for code snippets to create a Linux service monitoring script, I found several examples, but they didn’t work correctly when used with CRON. I want to show you guys how to create a Linux service monitoring script that works interactively and using CRON.

What is a Linux Bash script?

A Linux Bash script is a simple script that contains basic shell commands that you would type in at a Linux shell prompt. This makes it easy to get started with Linux Bash scripts. It is easy to throw commands into a shell script, just like you would interactively type into the shell prompt.

One of the great things about the Linux Bash script environment is it is built into Linux itself. There are no additional features you need to install to have the functionality. Also, you automatically have a task scheduler of sorts built into Linux to run the shell scripts with the Cron scheduler.

Create a Linux Service Monitoring Script

The process to create a Linux Service monitoring script is straightforward. You just need to have a code editor of sorts that can literally be notepad or some other text editor, and the sample code below. You can develop on the Linux server as well using nano, vi, or your editor of choice.

For the purposes of the demonstration of how to create a Linux service monitoring script, I will be monitoring Nginx to make sure the webserver is running. Let’s take a look at the script to do that.

Let’s walk through the script.

  1. At the top, we are defining some path variables
  2. The service nginx status command gets the status of the Nginx service
  3. We are outputting that to a file called output.txt
  4. Once we have created the output.txt file, we grep the contents of the file
  5. The grep command will look for the word “running”
  6. If it finds running we know the service is running
  7. If it doesn’t find the word running, it will move on to the “else” portion of the script
  8. In the else logic, we run the service nginx start command to start the service if it isn’t running
  9. We can then echo contents out to pipe into a notification email
  10. The fi statement defines the end of the script block
  11. At the very bottom we are removing the output.txt file to clean up after the script runs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin


#!/bin/bash


service nginx status > output.txt

if grep -q "running" output.txt; then    
echo "The Nginx service is running"
else
service nginx start 
echo "The Nginx service was stopped, errored, or inactive. The service has been started." | mail -s "The Nginx service was stopped" [email protected];
fi

sudo rm -f output.txt

Once you create a Linux monitoring script, you will need to save it. If you created it on a workstation outside of your Linux server, you will need to SCP the file locally to the Linux server to use. Using the example code above, I saved the script as checkservice.sh.

Additional tools needed?

Really, the only additional tools you may need to install involve the mail functionality. If you do not have the mailutils package installed, you will need to install that:

apt-get install mailutils

You may choose to use another local mail server. Just know you will need to fulfill the requirements of the specific mail server to call it when you create a Linux monitoring script.

Using CRON with Linux Bash Script

You may be wondering, how do we call the script once we have created it? You can run the script interactively if you want to being with. I prefer to do this to make sure the syntax and everything is working correctly.

If you can’t seem to execute your shell script, it may be because it is not set with the execute attribute. To make a shell script executable, you can do that with the following command:

chmod +x

Once you have made the script executable, you can execute it to test. With the Nginx service running, when we execute the script, we can see it properly detects the service is running.

Running the linux service monitoring script
Running the linux service monitoring script

Below, I am simulating the Nginx service being stopped or failed for whatever reason. As you can see now, when we run the checkservice.sh script, it detects the service is not running and issues the service start nginx command.

Simulating a failure and running the check monitoring script
Simulating a failure and running the check monitoring script

Create a Linux Service Monitoring Script Video

If you would like to watch a video tutorial of how to create a Linux service monitoring script, watch my video walkthrough below.

Create a Linux Service Monitoring Script FAQs

  • What is a Bash script? A Linux Bash script is a simple human-readable script file that contains commands that you would type in manually at a shell prompt.
  • How can you run a Bash script automatically? You can call Bash scripts from other Bash scripts. You can also schedule these to run using the Cron functionality found in Linux.
  • What is Cron? Cron is the built-in scheduler found in Linux to run scripts. You can have Cron run Bash scripts automatically on a set interval and schedule.
  • Why monitor Linux services? Business-critical Linux services need to be monitored to ensure they are running correctly. Using a Linux script to watch a service and ensure it is running is a great way to have self-healing infrastructure.

Wrapping Up

Linux Bash scripts are extremely powerful and provide a great way to perform Linux tasks in an automated way. Hopefully, this quick and easy tutorial will help anyone who wants to get started with Linux shell scripting to see how you can easily start reaping the benefits of running scripts in Linux as part of your system administration. The process to create a Linux service monitoring script is only an example of what you can do.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

One Comment

  1. I would suggest using systemd for this, for example:

    /bin/systemctl | grep nginx | grep running | awk '{print $4}'

    To filter it’s status. Another syntax might work just fine. You can iterate through it so easily with a for loop.

    A better way to define your bash environment would be:

    #!/usr/bin/env bash

    Making scripts more ‘env proof’

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.