Your cart is currently empty!
How to Automate Daily Linux Health Checks with a Bash Script + Cron
System admins know this: small problems left unchecked turn into big ones. Keeping an eye on your Linux server’s health can catch those issues before they snowball. But let’s be real—nobody wants to log in every day and run a checklist by hand. The fix? Automation.
Here’s how you can automate daily Linux health checks using a simple Bash script and cron.
What Should You Check?
Before we script, let’s nail down what a “health check” should include. Common essentials:
- Disk usage
- Memory usage
- CPU load
- Uptime
- Critical service status (like SSH, web server, database)
- Recent errors in system logs
You can customize this list to fit your server and needs.
Step 1: Write the Bash Script
Below is a sample script, healthcheck.sh
. Tweak it as needed.
#!/bin/bash
REPORT="/var/log/daily_health_report.txt"
HOST=$(hostname)
{
echo "Linux Health Report for $HOST - $(date)"
echo "----------------------------------------"
echo
echo "Disk Usage:"
df -h
echo
echo "Memory Usage:"
free -m
echo
echo "CPU Load:"
uptime
echo
echo "Critical Services:"
systemctl is-active sshd
systemctl is-active apache2 2>/dev/null || systemctl is-active httpd 2>/dev/null
systemctl is-active mysql 2>/dev/null || systemctl is-active mariadb 2>/dev/null
echo
echo "Recent System Errors:"
journalctl -p 3 -b --no-pager | tail -n 10
echo
} > "$REPORT"
Tips:
- Save this file somewhere secure, e.g.,
/usr/local/bin/healthcheck.sh
. - Make it executable:
chmod +x /usr/local/bin/healthcheck.sh
.
Download the Script:
Script – https://mega.nz/file/KZ1UnQ6Z#dAA6rQetNFnDO3-jT-LaCNq9qVvyKc9yM_jGPzngsps
Step 2: Set Up Cron for Daily Checks
Open your crontab editor:
crontab -e
Add this line to run the script every day at 7am:
0 7 * * * /usr/local/bin/healthcheck.sh
That’s it—the script runs every morning, and you’ll get a fresh report at /var/log/daily_health_report.txt
.
Step 3: (Optional) Email the Report
If you want these reports in your inbox, add this line to your script after the closing }
:
mail -s "Daily Health Report for $HOST" [email protected] < "$REPORT"
Make sure you have mail
or mailx
installed and configured on your server.
Why Automate?
Automation saves you time and helps you catch issues early. Daily reports show trends and weird spikes before they cause downtime. Plus, with everything logged, you have a record to look back on.
Final Thoughts
A few lines of Bash and a cron job can save hours of manual work—and maybe save your server, too. Start small, tweak as needed, and enjoy the peace of mind that comes with knowing your Linux system is always being watched.
Need more detail, or want a customized health script? Drop a comment below!
Tech enthusiast and content creator passionate about making technology simple for everyone. I share practical tips, guides, and reviews on the latest in computers, software, and gadgets. Let’s explore the digital world together!