Skip to content
Home » Learn How to Detect Uptime Changes With a Bash Script

Learn How to Detect Uptime Changes With a Bash Script

Creating a bash script to detect and respond to system changes can be a powerful tool. In this blog, we’ll go over how to create such a script and explore some practical use-cases for it.

Creating the Script

The first step in creating our script is to decide what changes we want to detect. For this example, we’ll be detecting changes to the system’s uptime. To do this, we’ll use the uptime command and store the output in a variable.

#!/bin/bash

current_uptime=$(uptime)

Next, we’ll use a while loop to continuously check the uptime and compare it to the stored value. If the uptime has changed, we’ll output the new uptime to a file and also print it to terminal:

#!/bin/bash

current_uptime=$(uptime)

while true; do
    new_uptime=$(uptime)
    if [[ "$current_uptime" != "$new_uptime" ]]; then
        echo "Uptime has changed from:"
        echo "Current Uptime: $current_uptime"
        echo "New Uptime: $new_uptime"
        echo "Uptime has changed from $current_uptime to $new_uptime" >> uptime_changes.txt
        current_uptime=$new_uptime
    fi
    sleep 5
done

Note that we’re using a sleep command to give the system some time between each check, so we’re not constantly checking the uptime and causing unnecessary stress on the system.

Practical Use-Cases

There are many different ways this script can be useful, here are a few examples:

  • Monitoring server uptime: This script can be used to detect and log any downtime on a server, which can be useful for troubleshooting and identifying potential issues.
  • Tracking user activity: By modifying the script to detect changes to the last command, we can track when users log in and out of the system and log this information to a file.
  • Detecting and responding to changes in system resources: By using commands like free or top, we can detect changes in system resources such as memory usage or CPU usage and respond accordingly, for example by sending an alert or increasing resources.

Tips

Here are a few tips to keep in mind when creating your own bash script:

  1. Be specific in what changes you’re detecting: Only detect the changes that are relevant to your use-case, this will make your script more efficient and prevent unnecessary output.
  2. Test your script thoroughly: Before using your script in a production environment, make sure to test it in a controlled environment to ensure it works as expected.
  3. Use sleep commands: As mentioned earlier, using sleep commands will help prevent unnecessary stress on the system.
  4. Make use of the built-in commands: Bash has many built-in commands that can be used to gather information about the system, so make use of them.
  5. Keep it simple: Avoid using complex or convoluted logic, this will make your script easier to understand and maintain.

Challenge

Now that you’ve learned how to create a script to detect changes in the system’s uptime, your challenge is to modify the script to detect changes in memory usage. Once you’ve done that, try to respond to changes in memory usage by sending

Leave a Reply

Your email address will not be published. Required fields are marked *

ten + seventeen =