For system administrators managing Linux servers, ensuring high availability is a primary responsibility. While enterprise environments use complex monitoring suites like Nagios, Zabbix, or Prometheus, these tools are often overkill for smaller deployments, personal homelabs, or specific internal nodes. Sometimes, you simply need a lightweight, automated way to record exactly how long a server has been running, verify it hasn’t unexpectedly rebooted, and log that data for review. By writing a simple Bash script and automating it with cron, you can create a highly efficient uptime monitor without installing any third-party dependencies.
Step 1: Write the Uptime Monitoring Script
The core of this automation relies on the built-in uptime -p command, which outputs the system’s running time in a human-readable format, combined with the date command for timestamping.
- Log into your Linux server via SSH.
- Create a dedicated directory for your monitoring scripts (optional but recommended for organization):
mkdir -p ~/scripts/monitor cd ~/scripts/monitor - Use the nano text editor to create a new Bash script:
nano check_uptime.sh - Paste the following code into the editor. This script captures the current date and the system uptime, formats them into a single string, and appends that string to a log file located in the
/var/logdirectory (or your local home directory if you prefer not to use sudo).#!/bin/bash # Define the location of the log file LOG_FILE="/var/log/custom_uptime.log" # If you are not running this as root, change the path to: LOG_FILE="$HOME/custom_uptime.log" # Capture the current date and time CURRENT_DATE=$(date '+%Y-%m-%d %H:%M:%S') # Capture the system uptime (pretty format) SYS_UPTIME=$(uptime -p) # Format the log entry LOG_ENTRY="[$CURRENT_DATE] Server Status: $SYS_UPTIME" # Append the entry to the log file echo "$LOG_ENTRY" >> "$LOG_FILE" - Save the file (
Ctrl+O, Enter) and exit the editor (Ctrl+X).
Step 2: Make the Script Executable
Before Linux can run the script, you must grant it execution permissions.
chmod +x check_uptime.sh
You can test the script manually by running ./check_uptime.sh and then inspecting the log file using cat /var/log/custom_uptime.log (or your chosen path) to ensure the entry was written correctly.
Step 3: Automate the Script Using Cron
To make this a true monitoring solution, the script needs to run automatically at regular intervals. The cron daemon is the standard Linux tool for scheduling automated tasks.
- Open your user’s crontab file for editing:
crontab -e(If you configured the script to write to
/var/log, you must edit the root crontab usingsudo crontab -einstead). - Scroll to the bottom of the file and add a new line to define the schedule. To run the script automatically every hour, add the following line (ensuring you provide the absolute path to your script):
0 * * * * /home/username/scripts/monitor/check_uptime.sh - Save and exit the crontab editor. The cron daemon will immediately apply the new schedule.
Your Linux server will now quietly execute the script in the background at the top of every hour, building a continuous, lightweight log of the system’s stability that you can review at any time.