Introduction:
While running the website, the last thing you want is to go down for your Nginx web server without looking at you. Sudden service failure can mean downtime, lost traffic, and unhappy users. So, how can you ensure your process server is always up and running? With a simple shell script!
In this article, we will run you through a script that checks the status of your service process. If the service is not running, the script sends you a friendly email alert in the HTML format, providing all the details you need to return to the track quickly.
Objectives:
Monitor if your process server is running. If something goes wrong, get an immediate email alert. Customize the script easily to suit your requirements.
Prerequisites:
Before diving in the script, here are the things you need: A Unix-based system (Linux or MCOS). The email was installed and configured to send email from its server. Basic shell scripting knowledge (we will walk you through everything, there is no worry). script: Let’s break the script that will monitor your service process.
This script will check:
if the service is running, and if it is not, it will email you in a good HTML format. You will also get information about your server, such as host, IP address, and present time, so you know what is happening.
Script:
#!/bin/bash ################################################################## # Date : 08-Feb-2025 # Author: Krishna Tummeti # Website : TECH BASE HUB # Purpose: Monitor service # Get the hostname, IP address, and current date and time ################################################################## HOSTNAME=$(hostname) IP_ADDRESS=$(hostname -I | awk '{print $1}') CURRENT_DATE=$(date '+%Y-%m-%d %H:%M:%S') # Enter your email IDs recipient="krishnatummeti@gmail.com,krishnatummeti@outlook.com" # Mail subject subject="Problem - Nginx alert - $HOSTNAME - $CURRENT_DATE" # Function to generate HTML content for the email generate_html_content() { body="<html> <head> <style> body { font-family: Arial, sans-serif; } .container { width: 80%; margin: 20px auto; border: 1px solid #ccc; padding: 20px; border-radius: 5px; } .header { text-align: center; margin-bottom: 20px; } .status { font-size: 24px; color: #FF0000; font-weight: bold; text-align: center; margin-bottom: 20px;} .info { font-size: 16px; margin-bottom: 10px;} .action { margin-top: 20px; text-align: center;} .highlight { background-color: #f0f0f0; padding: 5px; border-radius: 3px; } .footer { text-align: center; margin-top: 20px; font-size: 12px; color: #666; } </style> </head> <body> <div class='container'> <div class='header'> <h2>Nginx service alert</h2> </div> <hr> <div class='info'><b>Hostname:</b> $HOSTNAME</div> <div class='info'><b>IP Address:</b> $IP_ADDRESS</div> <div class='info'><b>Current Date and Time:</b> $CURRENT_DATE</div> <div class='info'><b>Operating System:</b> Oracle Linux Server 8.10</div> <div class='info'><b>Severity: </b><span style='color: #FF0000;'>CRITICAL</span></div> <hr> <div class='status'>Nginx is NOT running!</div> <div class='action'>Action: Start the Nginx process using <code>sudo systemctl start nginx</code>.</div> <hr> <div class='footer'><p>This is an automated monitoring notification. Please take action immediately.</p></div> </div> </body> </html>" } # Function to send the email send_email() { echo -e "Subject: $subject\nMIME-Version: 1.0\nContent-Type: text/html\nTo: $recipient\n\n$body" | sendmail -t } # Main function to generate email and send it main() { generate_html_content send_email } # Function to check Nginx process status check_nginx_status() { NGINX_COUNT=$(ps -ef | grep nginx | grep -v grep| wc -l) if [ "$NGINX_COUNT" -eq "0" ]; then NGINX_STATUS="Nginx is NOT running!" echo "$NGINX_STATUS" main exit 1 # Exit if Nginx is not running else NGINX_STATUS="Nginx is running normally." echo "$NGINX_STATUS" exit 0 # Exit if Nginx is running fi } # Start by checking Nginx status check_nginx_status
Make the Script Executable:
chmod +x alret_scrpt.sh
Run the Script:
./alret_scrpt.sh
or
bash alret_scrpts.sh
How the script works:
Get server information:
The script begins by collecting some important details such as the server’s hostname, IP address, and current date/time
E-mail notification setup:
If the service does not run, it sends an e-mail to the specified recipients. E-post is styled in HTML for clarity and contains useful information about your server and problem (service does not run).
Check the process:
The script is being investigated whether the process searches for the PS command. If no procedures are found, the script sends an e -mail warning. If the service is running, the script ends without sending an e-post.
what do you get:
When the script finds out that the service is not running, you will receive an email with a wide HTML message that looks like this
Subject: Problem – alert – HOSTNAME – CURRENT_DATE
Content:
Hostname: Server host.
IP Address: Server IP Address.
Date/time: current date and time.
Severity: Important (because the service is down).
Status: service is not running.
Action: Start service using Sudo Systemctl Start NGINX.
E-mail notification:
ALSO READ :
- Automating Linux monitor memory usage script
- How to install Linux Server Monitoring tool Nagios XI and NCPA
- How to Automating User Switch in Shell Scripts with Nopasswd Sudoers
Conclusion:
This simple script helps you monitor your service and ensures that you are alerted immediately if something goes wrong. With a clean and well-structured email notification, you have all the information that you need to bring back the service and run in a short time. Feel free to turn the script to suit your specific needs, such as adjusting the email format or adding more checks!
Thank you!