
If you’ve spent any time managing Linux systems, you already know how repetitive and time-consuming some tasks can be. Whether it’s checking disk space, restarting failed services, or keeping your system updated, doing everything manually quickly becomes a headache, especially if you’re handling more than one server.
Bash scripts are like tiny assistants that help you automate common tasks, reduce human error, and save valuable time. Instead of running the same commands over and over again, you can let your scripts handle it – reliably and consistently.
Over the years, many system administrators have created and refined scripts to monitor systems, automate maintenance, and respond to issues before they become serious problems.
In this article, you’ll discover five simple but powerful Bash scripts that are useful in everyday Linux system administration. These scripts are beginner-friendly and easy to modify for your own environment.
1. Disk Usage Monitor Script
One of the most common issues on Linux servers is running out of disk space. Logs fill up, backups grow, and suddenly your app crashes because the server is out of space. That’s why my first script checks disk usage and sends an alert if usage goes beyond a set limit (say, 80%).
#!/bin/bash THRESHOLD=80 EMAIL="[email protected]" df -hP | grep -vE '^Filesystem|tmpfs|cdrom' | while read line; do USAGE=$(echo $line | awk '{print $5}' | sed 's/%//') MOUNTPOINT=$(echo $line | awk '{print $6}') if [ $USAGE -ge $THRESHOLD ]; then echo "Warning: High disk usage on $MOUNTPOINT ($USAGE%)" | mail -s "Disk Alert: $HOSTNAME" $EMAIL fi done
This script checks each partition, and if any of them cross the 80% threshold, I get an email. It helps me fix issues before they become problems. I run this script via cron every 6 hours.
2. System Update Automation Script
Keeping systems up to date is critical, especially for security patches. I use this simple Bash script to automatically update packages, clean up the system, and send me a report.
#!/bin/bash LOGFILE="/var/log/sys-updates.log" EMAIL="[email protected]" echo "Starting updates on $(date)" >> $LOGFILE apt update && apt upgrade -y >> $LOGFILE 2>&1 apt autoremove -y >> $LOGFILE 2>&1 tail -20 $LOGFILE | mail -s "System Update Report: $HOSTNAME" $EMAIL
(For RHEL/CentOS users, just replace apt
with yum
or dnf
commands.)
Running this script through a cron job once a day keeps my systems updated and clean. The email report gives me peace of mind that everything went smoothly. If something breaks, I can check the log file and roll back.
3. Service Health Checker Script
As a sysadmin, I need to know if key services like Apache, Nginx, or MySQL go down. This script checks whether a specific service is running, and if not, it restarts it and notifies me.
#!/bin/bash SERVICES=("apache2" "mysql") EMAIL="[email protected]" for SERVICE in "${SERVICES[@]}"; do if ! systemctl is-active --quiet $SERVICE; then systemctl start $SERVICE echo "$SERVICE was down and has been restarted on $HOSTNAME" | mail -s "Service Restart Alert" $EMAIL fi done
This script checks them every 5 minutes via cron. If any service is down, it restarts it automatically and sends me a heads-up.
4. Backup Script for Important Files
Backups are boring, until you need them. I have a custom Bash script that backs up my critical files (like web files, databases, config files) and stores them in a compressed archive.
#!/bin/bash BACKUP_DIR="/backup" SOURCE_DIRS="/etc /var/www /home" DATE=$(date +%F) BACKUP_FILE="$BACKUP_DIR/backup-$DATE.tar.gz" EMAIL="[email protected]" tar -czf $BACKUP_FILE $SOURCE_DIRS if [ $? -eq 0 ]; then echo "Backup completed successfully: $BACKUP_FILE" | mail -s "Backup Success - $HOSTNAME" $EMAIL else echo "Backup FAILED!" | mail -s "Backup Failed - $HOSTNAME" $EMAIL fi
I’ve had users accidentally delete important stuff, and this script has saved me more than once. I keep 7 days’ worth of backups and rotate them with another cleanup script. You can also upload backups to a remote server or cloud storage for more safety.
5. User Login Monitoring Script
This script checks for user login activity and alerts you if someone logs in, especially helpful if you manage production servers and want to track access.
#!/bin/bash LOGFILE="/var/log/auth.log" LAST_RUN_FILE="/tmp/last_run_time" EMAIL="[email protected]" if [ ! -f $LAST_RUN_FILE ]; then date --date='5 minutes ago' +%s > $LAST_RUN_FILE fi LAST_RUN=$(cat $LAST_RUN_FILE) NOW=$(date +%s) awk -v last=$LAST_RUN -v now=$NOW ' $0 ~ /session opened for user/ { cmd = "date -d ""$1" "$2" "$3"" +%s" cmd | getline t close(cmd) if (t >= last && t <= now) { print $0 } } ' $LOGFILE | mail -s "Login Alert - $HOSTNAME" $EMAIL echo $NOW > $LAST_RUN_FILE
This script helps me know who accessed the server and when. It’s great for detecting unusual access patterns. You can expand it to block IPs or trigger alarms if needed.
Conclusion
In conclusion, relying on Bash scripts in my daily sysadmin routine has significantly improved how I manage and maintain Linux systems. These scripts may seem simple on the surface, but they handle critical tasks that keep servers stable, secure, and running smoothly.
If you’re looking to automate full system health checks (CPU, memory, disk, and more), don’t miss my other guide: How to Automate Daily Linux Health Checks with a Bash Script + Cron