Day 5: Automating Tasks with Bash Scripts: Part 1

Hey there, fellow Linux explorers! 👋 Welcome back to Day 5 of our DevOps in 90 Days series, where we dive into the world of automation using the magic of Bash scripting. If you’re tired of doing repetitive tasks manually and want to unleash the full potential of automation, buckle up. It’s time to write some scripts that not only save time but also make you feel like a superhero 🦸♂️ when your system runs on autopilot!
Why Bash Scripts?
Bash scripts are the Swiss Army knives of Linux. They can automate pretty much anything, from system maintenance to complex workflows. If you’re working in a DevOps or SysAdmin role (like me 🙋♂️), automating routine tasks is essential. Today, we’ll start by tackling disk space monitoring and service health checking — two vital tasks for keeping your system in check. 🚀
Let’s dive right into two awesome scripts!
Script 1: Disk Usage Monitoring and Alerts 🚨
Ever faced the horror of your / partition filling up unexpectedly? 😱
Or maybe your /home directory is hogging space like it’s preparing for the apocalypse. Well, no more sleepless nights! Here’s a simple script that checks if your root partition (/) is 90% full or if the /home directory exceeds 2 GB. If either condition is met, it’ll shoot an email to the admin to sound the alarm! 📧
The Script:
#!/bin/bash
# Admin email address
ADMIN="[email protected]"
# Get the percentage of used space on /
ROOT_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
# Check if the / partition usage is 90% or more
if [ "$ROOT_USAGE" -ge 90 ]; then
echo "Warning: / partition usage is at ${ROOT_USAGE}%!" | mail -s "/ Partition Warning" $ADMIN
fi
# Check the size of the /home directory (in GB)
HOME_SIZE=$(du -sh /home | awk '{print $1}' | sed 's/G//g')
# Check if /home size exceeds 2GB
if (( $(echo "$HOME_SIZE > 2" | bc -l) )); then
echo "Warning: /home size is ${HOME_SIZE}GB, exceeding 2GB!" | mail -s "/home Directory Warning" $ADMIN
fiHow it works:
- Disk usage check: The script uses the
dfcommand to monitor the usage of the/partition. If it’s over 90%, it sends a warning email. - Directory size check: The script is used
duto calculate the size of the/homedirectory. If it’s over 2 GB, it sends another email. - Email notifications: The
mailcommand sends an alert to the system admin, allowing them to act before things get out of hand.
Setting Up the Script:
- Save the script as
disk_monitor.sh. - Make it executable:
chmod +x disk_monitor.shSchedule it to run periodically using cron. For example, to run the script every hour:
crontab -e
Add this line:
0 * * * * /path/to/disk_monitor.shThat’s it! You’ll now receive an email whenever your disk usage crosses critical levels, saving your system from running out of space.
Script 2: Monitoring and Restarting Services 🛠️
Imagine this: You’re working on a project, and suddenly, the web server goes down. 😱 What if you could have a script that automatically checks whether a service is running and, if it isn’t, restarts it for you? Say goodbye to manual restarts!
The Script:
#!/bin/bash
# Define the service you want to monitor
SERVICE="apache2"
# Check if the service is running
if ! systemctl is-active --quiet $SERVICE; then
# If not running, start the service
systemctl start $SERVICE
# Send an email notification to the admin
echo "The $SERVICE service was down and has been restarted." | mail -s "$SERVICE Service Restarted" [email protected]
fiHow it works:
- Service check: The script checks if the defined service (in this case,
apache2) is running usingsystemctl is-active. If it’s not running, it starts the service. - Auto-restart: If the service is found to be down, the script restarts it with
systemctl start. - Notification: Once the service is restarted, an email is sent to the admin, letting them know about the downtime and the restart action.
Setting Up the Script:
- Save the script as
service_monitor.sh. - Make it executable:
chmod +x service_monitor.sh- Schedule it to run every minute using
cron:
crontab -e
- Add this line to check the service status every minute:
* * * * * /path/to/service_monitor.shNow, your system will automatically monitor the service and restart it whenever necessary. You can easily customize this script for any other services you rely on, such as nginx, mysql, or even custom services.
Script 3:Monitor System Health ( Disk.Memory,CPU)
#! /bin/bash
TIME=$(date "+%Y-%m-%d %H:%M:%S")
echo -e "Time\tMemory\t\tDisk /root\tCPU"
seconds="3600"
end=$((SECONDS+seconds))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf("%.f\n", 100 - $1)}')
echo -e "$TIME\t$MEMORY$DISK$CPU"
sleep 3
doneThis script is designed to monitor system resource usage (Memory, Disk, and CPU) for a period of one hour (3600 seconds). Here’s how the script works, and I’ll explain each part in detail:
Script Explanation:
#!/bin/bash- Shebang: This tells the system to use Bash to run the script.
TIME=$(date "+%Y-%m-%d %H:%M:%S")- TIME Variable: Captures the current date and time in the format
YYYY-MM-DD HH:MM:SS. This value is displayed with each iteration of the resource check.
echo -e "Time\t\t\tMemory\t\tDisk /root\tCPU"- Header Output: Prints a table header for the output: Time, Memory, Disk usage of
/root, and CPU usage.
seconds="3600"
end=$((SECONDS+seconds))- Timer Setup: This sets the script to run for 3600 seconds (one hour). The
SECONDSvariable is a special Bash variable that tracks how long the script has been running.
while [ $SECONDS -lt $end ]; do- While Loop: This loop will keep running until the current
SECONDSvalue exceeds the total time set byend.
Resource Monitoring:
Memory Usage:
MEMORY=$(free -m | awk 'NR=2{printf "%.f%%\t\t", $3*100/$2 }')- Command:
free -mshows memory usage in megabytes. - Explanation: The
awkcommand selects the second line (main memory line) and calculates the memory usage percentage by dividing used memory ($3) by total memory ($2), then prints it with%.ffor no decimal points.
Disk Usage:
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')- Command:
df -hshows disk space usage. - Explanation: The
awkcommand looks for the line where the mount point is/, and extracts the disk usage percentage ($5).
CPU Usage:
CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf("%.f\n", 100 - $1)}')- Command:
top -bn1gets real-time CPU usage data. - Explanation: The
grepandsedcommands extract the idle CPU percentage, then the script subtracts that value from 100 to get the CPU usage percentage. The result is formatted to show whole numbers (%.f).
Output:
echo -e "$TIME\t$MEMORY$DISK$CPU"- Display: This line prints the current time, memory usage, disk usage, and CPU usage in one line, aligned with the header.
Wait and Repeat:
sleep 3
done- Sleep: The script waits for 3 seconds between checks. After this pause, it repeats the loop until the total time of 3600 seconds has passed.
Example Output:
Time Memory Disk /root CPU
2024-10-05 14:10:00 67% 40% 23
2024-10-05 14:10:03 68% 40% 25
2024-10-05 14:10:06 67% 40% 21Improvements You Can Make:
- Log to a File: You can direct the output to a log file for later analysis.
echo -e "$TIME\t$MEMORY$DISK$CPU" >> /path/to/logfile.txtAlert System: You could add a check for high CPU or disk usage, and send an alert (email or notification) when a threshold is exceeded.
Let me know if you want to modify or expand this script!
Need more fun ! Checkout below scripts 👇
Wrapping Up✌️
And that’s a wrap for Part 1 of Automating Tasks with Bash Scripts! With just a few lines of Bash code, you’ve automated two critical tasks:
- Monitoring disk space and alerting you before it’s too late.
- Keeping services alive by automatically restarting them if they crash.
These scripts save you the headache of manual monitoring and provide peace of mind knowing that your system will keep running smoothly. In Part 2, we’ll dive deeper into more complex automation, covering topics like log rotation, backup scheduling, and automated security audits.
Until next time, happy scripting! 🤓
Want to Learn More?
If you enjoyed this post and want more hands-on scripts, feel free to check out my other articles and join me in exploring the endless possibilities of Linux automation. Let’s turn you into a Bash scripting ninja! 🐱👤
Stay tuned for more DevOps awesomeness!
✅✅feel free to connect with us.
LinkedIn: https://www.linkedin.com/in/karthick-dkk/
Follow my Medium Account (To get valuable information)
For more updates: subscribe to this medium account.
Follow for more: ✌️





