01Create the script
- In the Terminal, use a text editor like
nanoorvimto create a new script file. In this example, I'll usenano:
``bash nano monitor_website.sh ``
- Paste the following script into the
nanoeditor:
```bash #!/bin/bash
url="http://yourwebsite.com"
while true; do statuscode=$(curl -o /dev/null -s -w "%{httpcode}\n" $url) if [ "$statuscode" -ne 200 ]; then echo "$(date) - Website $url is down with status code $statuscode" fi sleep 60 done ```
Be sure to replace http://yourwebsite.com with the URL of the website you want to monitor.
- Press
Control+Oto save the file, then pressEnterto confirm the filename. PressControl+Xto exitnano.
02Make 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.shUnderstanding 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.
03Run 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.
04Move 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] 12345In 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 105Stop 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 12345If 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:
- 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:
``bash [1] 12345 ``
In this case, 12345 is the PID.
- If you didn't note the PID or have closed the terminal, you can find the PID by using the
pscommand andgrepto filter the results. Here's how you would do that:
``bash ps aux | grep monitor_website.sh ``
This command will output something like this:
``bash 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.
Once you have the PID, you can use the
killcommand to stop the script:$ kill 12345Replace 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.