Hey there, tech enthusiasts! In this blog post, we'll embark on a journey to create a robust website monitoring system using the Bash shell. We'll dive into the intricacies of checking website availability, handling timeouts, and sending notifications via curl when things go awry.
-
Laying the Foundation:
Begin by opening your favorite terminal emulator and creating a new Bash script file. We'll call it "website_monitor.sh." This script will serve as the brain of our monitoring system.
-
Defining the Target:
In this script, we'll define the target website we want to monitor. Let's say we want to keep an eye on "example.com." Go ahead and add this line to the script:
TARGET_WEBSITE="example.com"
-
Crafting the Check Function:
Now, we'll create a function called "check_website" that will handle the actual monitoring. This function will ping the website, check for a 200 status code, and measure the response time. Here's the code:
check_website() { STATUS_CODE=$(curl -sL -w "%{http_code}" -o /dev/null $TARGET_WEBSITE) RESPONSE_TIME=$(curl -sL -w "%{time_total}" -o /dev/null $TARGET_WEBSITE) }
-
Handling Timeouts and Errors:
We'll enhance our script to handle timeouts and other errors that may arise during the monitoring process. We'll use the "timeout" command to set a timeout limit and capture any errors using the "curl" command's exit status.
check_website_with_timeout() { if timeout 5 curl -sL -o /dev/null $TARGET_WEBSITE; then echo "Website '$TARGET_WEBSITE' is up (Status Code: $STATUS_CODE, Response Time: $RESPONSE_TIME seconds)." else echo "Error: Website '$TARGET_WEBSITE' is down or timed out after 5 seconds." fi }
-
Scheduling Regular Checks:
To keep a constant eye on our website, we'll utilize Cron, a powerful tool for scheduling tasks. Add this line to your script to run the monitoring function every 5 minutes:
*/5 * * * * check_website_with_timeout >> /var/log/website_monitor.log
-
Sending Notifications:
Finally, we'll integrate curl to send notifications when the website goes down or becomes unresponsive. We'll use a simple email notification as an example.
notify_via_email() { MESSAGE="Website '$TARGET_WEBSITE' is down or timed out after 5 seconds." curl -X POST -F "to=you@example.com" -F "subject=Website Down Alert" -F "text=$MESSAGE" https://example.com/email-api }
Replace "you@example.com" with your email address and "example.com/email-api" with the URL of your email API.
-
Putting it All Together:
Now, save the script, make it executable, and schedule it using Cron. Sit back and relax as your Bash script diligently monitors your website and sends you alerts when necessary.
Conclusion:
With this Bash script, you have a reliable website monitoring system at your disposal. It will keep a watchful eye on your website, sending you timely notifications when things go awry. So, go ahead, implement this script and enjoy the peace of mind knowing that your website's uptime is in good hands.
Here is another scripts to monitor a website:
#!/bin/bash
# Define the website to monitor
website="https://example.com"
# Define the timeout in seconds
timeout=10
# Define the curl command to send the notification
notification_command="curl -X POST -d 'message=Website is down!' https://example.com/notify"
# Check if the website is responding
response=$(curl -s --max-time $timeout $website)
# Check the status code of the response
status_code=$(echo $response | head -n 1 | awk '{print $2}')
# Check if the website is up and return a 200 status code
if [[ $status_code -eq 200 ]]; then
echo "Website is up and responding with status code $status_code"
else
echo "Website is down or not responding with status code $status_code"
# Send the notification
$notification_command
fi
To use this script, you can save it as a file with a .sh
extension, for example, monitor_website.sh
. Then, you can make the script executable by running the following command:
chmod +x monitor_website.sh
Finally, you can run the script periodically using a cron job. For example, to run the script every 5 minutes, you can add the following line to your crontab:
*/5 * * * * /path/to/monitor_website.sh
Replace /path/to/monitor_website.sh
with the actual path to the script file.