12pm: Becoming a Terminal Watchdog: Monitoring Websites Through the Command Line

Ok so you've built a website, your code is safe in GitHub and it's available on the internet for all to see. But how do you know the website is up and running? Let's dive into the world of website monitoring. Using the terminal to monitor a website isn't just about being cool (which it is), it's about control, customisation, and automation. So, let's dive right in.

What does this article replace?

Ever relied on a website and found yourself constantly refreshing the page to check its status? Not anymore. This guide replaces manual refreshes with an automated, real-time monitoring system, all from your terminal.

How long will it take to set up?

This setup is a quick one! You should be able to have your terminal-based watchdog up and running in about 15 minutes.

4 out of 5 stars

Difficulty Rating:

Implementing real-time terminal feedback on your website's status is an advanced task, earning a difficulty rating of 4 out of 5. It goes beyond using existing tools and involves creating your own solution to provide real-time updates.

Step 1: Create the script

  1. In the Terminal, use a text editor like nano or vim to create a new script file. In this example, I'll use nano:

    nano monitor_website.sh
    
  2. Paste the following script into the nano editor:

    #!/bin/bash
    
    url="http://yourwebsite.com"
    
    while true; do
        status_code=$(curl -o /dev/null -s -w "%{http_code}\n" $url)
        if [ "$status_code" -ne 200 ]; then
            echo "$(date) - Website $url is down with status code $status_code"
        fi
        sleep 60
    done
    

    Be sure to replace http://yourwebsite.com with the URL of the website you want to monitor.

  3. Press Control+O to save the file, then press Enter to confirm the filename. Press Control+X to exit nano.

Step 2: Make the script executable

By default, the script you just created is a simple text file. You need to make it executable to run it as a script. You can do this with the chmod command:

chmod +x monitor_website.sh

Understanding foreground and background processes

Before we continue, let's clarify what we mean by running a process in the 'foreground' and the 'background'.

In the context of a terminal, a foreground process occupies the terminal while it runs. That is, while the process is running, you won't be able to enter any other commands into the terminal until the process completes its execution or is stopped or paused.

On the other hand, a background process runs independently of the terminal. This means you can start a process (like our monitoring script), move it to the background, and continue using the terminal for other tasks. The background process will continue to run until it's done, or until it's stopped.

In our website monitoring script, we start the script in the foreground to see the status messages live. But suppose we want to keep the script running but need to do some other work in the same terminal. In that case, we can send the process to the background using the & operator, like so:

Copy code
./monitor_website.sh &

When a process is running in the background, it won't display its output in the terminal. However, our script is designed to report website status, so how do we see these messages? One way is to bring the process back to the foreground using the fg command. This will allow us to see the status messages in real-time again.

Remember, understanding how to manage foreground and background processes can be a powerful tool in your command line toolbox, allowing you to multitask and run long tasks without tying up your terminal.

Step 3: Run the script in the background

Now you can run the script in the background with the following command:

./monitor_website.sh &

This command will start the script and send it to the background. The script will check the website every 60 seconds and print a message to the terminal if the HTTP status code is not 200. The message will contain the current date and time, the URL of the website, and the status code.

Step 4: Move the script to the foreground (Optional)

If you want to bring the script to the foreground, you can use the fg command. When you started the script, the terminal gave you a job number, which looked something like this:

[1] 12345

In this case, 1 is the job number, and 12345 is the process ID (PID). To bring the script to the foreground, use the fg command with the job number:

fg 1

Step 5: Stop the script

To stop the script, you can use the Control+C keyboard shortcut if the script is running in the foreground. If the script is in the background, you need to use the kill command with the PID:

kill 12345

If you've started the script in the background using the & symbol, you can stop it using the kill command.

Here are the steps to do that:

  1. You need to know the process ID (PID) of the script. If you've recently started it and haven't closed the terminal, you could find it in the output right after you started the script. It would look something like this:

    [1] 12345
    

    In this case, 12345 is the PID.

  2. If you didn't note the PID or have closed the terminal, you can find the PID by using the ps command and grep to filter the results. Here's how you would do that:

    ps aux | grep monitor_website.sh
    

    This command will output something like this:

    user   12345  0.0  0.0  4267768   688 s000  S    10:04PM   0:00.00 /bin/bash ./monitor_website.sh
    

    The second column is the PID.

  3. Once you have the PID, you can use the kill command to stop the script:

    kill 12345
    

    Replace 12345 with your actual PID.

This will terminate the script and stop monitoring the website.

That's it! You now have a script that will monitor a website, output a message to the terminal if the website is down, and can be run in either the background or the foreground.


With this handy terminal tool, you're in control. The terminal isn't just for commands, it's for creating solutions tailored to your needs. Stay tuned for the next part in our 24-hour terminal series.