Maintaining website uptime is essential for a positive user experience, as even short periods of downtime can frustrate users and result in lost business. Automating uptime checks on a Linux machine allows quick detection of issues, enabling faster response times. In this article, we’ll explore simple, effective ways to create a Website Uptime Checker Script in Linux using different commands like curl, wget, ping.
As my team and we are worked on windows machines and familiar with PowerShell but now we are working on the Linux based machine which lead to write articles based on command which we are using on daily basis.
1. Checking Website Uptime with curl
One of the most straightforward ways to check if a website is up is by using curl. The following multi-line bash script pings the specified website and returns its status:
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash website="https://example.com" # Check if website is accessible if curl --output /dev/null --silent --head --fail "$website"; then echo "Website is up." else echo "Website is down." fi |
Alternatively, here’s a one-liner with curl:
1 2 3 |
curl -Is https://dotnet-helpers.com | head -n 1 | grep -q "200 OK" && echo "Website is up." || echo "Website is down." |
Explanation:
- curl -Is sends a HEAD request to retrieve only headers.
- head -n 1 captures the status line of the HTTP response.
- grep -q “200 OK” checks if the response is “200 OK”.
Based on this, the command outputs either “Website is up.” or “Website is down.”
2. Monitoring Uptime with wget
If curl isn’t available, wget can be an alternative. Here’s a multi-line script using wget:
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash website="https://dotnet-helpers.com" if wget --spider --quiet "$website"; then echo "Website is up." else echo "Website is down." fi |
And the one-liner version with wget:
1 2 3 |
wget --spider --quiet https://dotnet-helpers.com && echo "Website is up." || echo "Website is down." |
Explanation:
- The –spider option makes wget operate in “spider” mode, checking if the website exists without downloading content.
- –quiet suppresses the output.
3. Checking Server Reachability with ping
Although ping checks the server rather than website content, it can still verify server reachability. Here’s a multi-line script using ping:
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash server="example.com" if ping -c 1 "$server" &> /dev/null; then echo "Server is reachable." else echo "Server is down." fi |
1 2 3 |
ping -c 1 https://dotnet-helpers.com &> /dev/null && echo "Server is reachable." || echo "Server is down." |
Summary
By combining these single-line and multi-line commands, you can monitor website availability, server reachability, and port status effectively. Monitoring website uptime on a Linux machine is simple and effective with these commands. Choose the single-line or multi-line scripts that best suit your needs, and consider automating them for consistent uptime checks. Start implementing these methods to ensure your website remains accessible and reliable for your users.