Server Watchdog

Maintaining maximum uptime for your website is crucial for providing an uninterrupted user experience in today's digital landscape. While there are many third party solutions designed to monitor uptime (for a fee most of the time), you still need to ensure that downtimes are tackled quickly and customers are unaffected.  

In this post, we'll introduce a script that leverages the restartsrv utility to act as a real-time website uptime watchdog. This script will monitor your website at designated time intervals, send an email alert with the status code if the website is down, and perform a graceful service restart - without risking loss of data. We will also cover step-by-step instructions on implementing the script on WHM via the terminal or remotely via SSH, testing the script by simulating a website outage, and monitoring the cron log in real time to ensure the script is running as planned.

Script Overview: The following script uses the restartsrv utility, which is available on cPanel & WHM servers, to check the status of the target website at specified time intervals. If the website is down, the script will send an email alert with the status code and attempt a graceful restart of the relevant service (e.g., Apache, NGINX).

#!/bin/bash

TARGET_URL="https://example.com"
SERVICE_TO_RESTART="httpd"
TIME_INTERVAL="5m"
EMAIL_ADDRESS="your@email.com"

while true; do
  STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" $TARGET_URL)

  if [ "$STATUS_CODE" -ne "200" ]; then
    echo "Website is down with status code: $STATUS_CODE" | mail -s "Website Down Alert" $EMAIL_ADDRESS
    /usr/local/cpanel/scripts/restartsrv $SERVICE_TO_RESTART --graceful
  else
    echo "Website is up and running with status code: $STATUS_CODE" | mail -s "Website Up and Running" $EMAIL_ADDRESS
  fi

  sleep $TIME_INTERVAL
done

 

Replace https://example.com with the URL of the website you wish to monitor, httpd with the appropriate service name, 5m with the desired time interval between checks, and your@email.com with the email address where you want to receive alerts.

Step 1: Implementing the Script on WHM via Terminal or Remotely via SSH

    1. Log in to your WHM server via the terminal or SSH.
    2. Create a new script file using your preferred text editor (e.g., nano, vim)
nano uptime_watchdog.sh

3. Copy the script provided above into the text editor and save the file

4. Make the script executable:

chmod +x uptime_watchdog.sh

5. Move the script to a suitable location, such as /usr/local/bin/:

mv uptime_watchdog.sh /usr/local/bin/

Step 2: Testing the Script by Simulating a Website Outage

  1. Temporarily modify your website's configuration or stop the relevant service to simulate an outage.
  2. Manually run the script
/usr/local/bin/uptime_watchdog.sh

3. Verify that the script successfully sends an email alert with the status code, restarts the service, and brings your website back online.

 

Step 3: Monitoring the Cron Log in Real Time to Check if the Script is Running as Planned

  1. Open a new terminal or SSH session.
  2. Run the following command to monitor the cron log in real time
tail -f /var/log/cron

3. Look for entries indicating that the script is being executed at the desired time intervals.

 

With this enhanced script, you can automate website uptime monitoring, receive email alerts when your website is down, and ensure that your site remains available to users with graceful restart capabilities - all without having to dish out exorbitant fees charged by third party monitoring and automation services. 

If you have any questions or are seeking support with implementing this service on your panel, get in touch with us and our team would be happy to help!

  • 1 Users Found This Useful
Was this answer helpful?