Automated Server Monitoring and Recovery with Bash Scripting — Prevent Downtime!

Rhegisan Jebas
4 min readMar 7, 2023

--

INTRODUCTION:

In this blog post, we will discuss a Bash script that can be used to monitor the status of an HTTP server and automatically restart it if it’s down. The script checks the status of the server by checking if its process ID (PID) is currently running, and if it’s not, it will start the server and send an email notification to the administrator.

SCRIPT:

#!/bin/bash
service_name=httpd
server_status=$(pidof $service | wc -l )
if [ $server_status -gt 0 ]
then
echo "$service_name is up and running"
else
echo "$service_name is down/dead"
systemctl start $service_name
echo "$service_name service is up and running now" | mail -s "$service_name service is down and restarted it now On $(hostname)" <your_email_id>@gmail.com
fi

SCRIPT EXPLANATION:

Let’s take a closer look at the code and explain how it works:

#!/bin/bash

This line tells the system to use the Bash shell to execute this script.

service_name=httpd

Here, we set the name of the service we want to monitor to “httpd”. You can change this to the name of the service you want to monitor.

server_status=$(pidof $service | wc -l )

This line runs the command pidof $service, which looks for the process ID (PID) of the process with the name specified in the $service variable. The output of pidof is a list of PIDs separated by whitespace. The output is then piped to wc -l, which counts the number of lines in the output (which should be the number of PIDs). The result is stored in the service_status variable.

when service is up the result is 1

httpd service is up and running.

when service is down/dead the result is 0

httpd service is down/dead.
if [ $server_status -gt 0 ]
then
echo "$service_name is up and running"
else
echo "$service_name is down/dead"

This block of code checks the value of “server_status”. If it’s greater than zero, it means that the server is running, so we print a message saying that the server is up and running. If “server_status” is not greater than zero, it means that the server is down, so we print a message saying that the server is down/dead and proceed to restart it.

systemctl start $service_name
echo "$service_name service is up and running now" | mail -s "$service_name service is down and restarted it now On $(hostname)" <your_email_id>@gmail.com
fi

This block of code starts the service using the “systemctl” command and sends an email notification to the administrator to inform them that the service was down and has been restarted. Replace “<your_email_id>@gmail.com” with the email address where you want to receive the notification.

NOTE :- The script assumes that the systemctl command is available and that the user running the script has permission to start the server service. If you're running the script on a system that doesn't use systemctl, use service and if the system doesn’t use service, you'll need to modify the script accordingly.

service $service_name start
echo "$service_name service is up and running now" | mail -s "$service_name service is down and restarted it now On $(hostname)" <your_email_id>@gmail.com
fi

AUTOMATING THE SERVICE MONITORING:

Adding a cron job will allow this script to run automatically at set intervals to continuously monitor the HTTP server and take action if it’s down.

To add a cron job, follow these steps:

  1. Open your terminal and type crontab -e to edit your user's crontab file.
  2. Add the following line to the crontab file to run the script every 5 minutes:
*/5 * * * * /path/to/your/script.sh

This line tells cron to run the script every 5 minutes. Replace “/path/to/your/script.sh” with the actual path to your script file.

3. Save the crontab file and exit the editor.

Now, the script will run every 5 minutes and check the status of the HTTP server. If the server is down, the script will start it and send an email notification to the administrator. This automated monitoring and recovery process helps ensure that your HTTP server is always available and can help prevent downtime.

CONCLUSION:

In this blog post, we discussed a Bash script that can be used to monitor the status of an HTTP server and automatically restart it if it’s down. This script is useful for ensuring that critical services are always available and can help prevent downtime. By understanding how this script works, you can modify it to monitor other services and customize the email notification to suit your needs.

Thanks for reading! I hope you found it helpful and informative.

If you have any questions or comments, please don’t hesitate to leave them in the comments section below. I’ll do my best to respond to each one.

If you’d like to stay up to date on my latest automation-related content, be sure to follow me on LinkedIn. I’d love to connect with you and hear your thoughts on automation in the workplace.

--

--

Rhegisan Jebas
Rhegisan Jebas

Written by Rhegisan Jebas

Passionate DevOps engineer sharing insights on building high-quality software through automation, containerization, and continuous delivery.

No responses yet