Recently I ran into some some problems with an HTTPS (SSL) website hosted on a Linux server running CPanel. In a nutshell, HTTPS worked fine initially - but it would stop working every morning when CPanel's Apache restart/cleanup cron jobs ran.
As a result, I needed a way to automate the process of checking whether a specific HTTPS URL was reachable, and restarting Apache if not. There are numerous scripts online that do almost what I needed, but the majority use the "ping" command and therefore only check if the server itself is up (telling you nothing about the availability of specific URLs or services on that server).
Read more for the details - and the script.
As is often the case, the quickest option was to find a pre-existing script that did 90% of what I needed, and then make some minor modifications & additions. My starting point was a ping script that's available here - and here is the modified version:
#!/bin/bash # Simple SHELL script for Linux and UNIX system monitoring with # ping command # ------------------------------------------------------------------------- # Copyright (c) 2006 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # Setup email ID below # See URL for more info: # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html # -------------------------------------------------------------------------# add ip / hostname separated by white space HOSTS="https://mysecuredomain.com http://mydomain.com"
# number of httpings to send COUNT=1
# command to restart the server if one of the URLs is unavailable # uncomment the command you want to use
# restart the server using the standard apache command # RESTART="httpd -k restart"
# restart the server using wget to run cpanel's apache restart script RESTART="http://username:password@mycpanelserver.com:2086/scripts/reshttpd?confirm=1"
# email report when SUBJECT="httping failed" EMAILID="myemail@mydomain.com" for myHost in $HOSTS do count=$(httping -c $COUNT $myHost | grep '100.00% failed' | wc -l) if [ $count = 1 ]; then # 100% failed echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID $RESTART fi done
For the most part, the script commands should be self-explanatory to anyone who's familiar with the UNIX command line. In a nutshell, it uses the program "httping" (ping for HTTP URLs) to check whether or not a specified URL is available - and if not, then the script will send an EMail alert and attempt to restart Apache.
INSTRUCTIONS
1) First, your server needs to have the "httping" software installed; if you don't have it already, it can be downloaded from here.
2) Next, copy the text of the script (above), save it as file, and then upload it to your webserver.
3) Configure the script for your specific setup. There are several configuration variables that you will need to edit before you can use the script. Change the "HOSTS" variable to URLs that you want to check (to check multiple URLs, just separate them with spaces). The COUNT variable defines how many ping attempts the script will make, the default value (1) should be fine for most cases. The "RESTART" variable controls the command use to restart the server, if one of the URLs isn't reachable - one option uses the CPanel restart script (the default), the other uses the standard "httpd -k restart" command.
4) If you wish to receive EMail reports when the specified URL(s) isn't reachable, then you'll also need to configure the SUBJECT and EMAILID variables. The SUBJECT variable controls the subject line, while EMAILID should contain the EMail address where you want the alerts to be sent.
5) Once the script has been configured, test it to make sure that it's working as it should. While testing, I strongly recommend commenting-out the "$RESTART" line, so that the script only sends an EMail alert and doesn't actually attempt to restart the server (and un-comment it only when you're sure that everything is working). And last, you'll probably want to setup a cronjob to automatically run the script periodically.
If you have any suggestions for improving this script (or if you spot any mistakes I've made), then please leave a comment or send us an EMail. We'll give credit for any changes/additions that we use.
Linux and Windows web hosting plans start at just $7.95/mo.