The Bash Loop
A No-Dependency System Monitor You Can Run in Cron
Hey engineer,
Want basic system telemetry like CPU, RAM, disk usage, logged to a rolling file every minute, without installing anything?
Let’s build a mini agent in Bash. Drop it into cron and watch it do its thing. Perfect for low-touch servers, test boxes, or inside CI/CD runners.
The Script
#!/bin/bash
# Set locale for consistent command output parsing
export LANG=C
# Define log file path
LOG_FILE="/var/log/syswatch.log"
# Check if log file is writable or can be created
if [ ! -w "$LOG_FILE" ] && [ ! -e "$LOG_FILE" ]; then
# Try to create the file
touch "$LOG_FILE" 2>/dev/null
fi
if [ ! -w "$LOG_FILE" ]; then
echo "Error: Cannot write to log file '$LOG_FILE'. Permission denied." >&2
exit 1
fi
# Get timestamp
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
# Get CPU usage (subtract idle percentage)
CPU=$(top -bn1 | grep "Cpu(s)" | awk -F'id,' '{ split($1, vs, ","); sub("%", "", vs[length(vs)]); print 100 - vs[length(vs)]"%"}')
# Get memory usage
MEM=$(free -m | awk '/Mem:/ {printf("%.2f%%", $3/$2 * 100.0)}')
# Get disk usage on root
DISK=$(df -h / | awk 'NR==2 {print $5}')
# Get system load average
LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | sed 's/^ *//')
# Write to log file
echo "$TIMESTAMP | CPU: $CPU | MEM: $MEM | DISK: $DISK | LOAD: $LOAD" >> "$LOG_FILE"What It Tracks
CPU usage (total active)
Memory usage (percent)
Disk usage of root mount
Load average
Log line example:
2025-05-19 07:45:01 | CPU: 12.5% | MEM: 62.14% | DISK: 77% | LOAD: 0.47, 0.59, 0.65How To Use
Save it as
/usr/local/bin/syswatch.shMake it executable:
chmod +x /usr/local/bin/syswatch.shAdd it to cron:
* * * * * /usr/local/bin/syswatch.sh
Logs updated every minute in /var/log/syswatch.log
Why It’s Useful
Zero overhead monitoring
Works on fresh VMs, test servers, containers
Log file is parse-friendly (CSV or grep)
Can be tailed with
multitailor piped into Slack alertsGreat lightweight solution before you install Prometheus
Simple. Scriptable. Observability, without the complexity.
I built the DevOps Automation Vault , a hand-crafted set of 60+ production-ready scripts for backups, monitoring, security, cleanup, and more.
Every script is cron-ready, supports dry-run mode, and works right out of the box.
👉 Grab your copy now →

