Top 10 Bash Scripts for Every DevOps Engineer
Eva Clari

Eva Clari @eva_clari_289d85ecc68da48

About: I’m Eva Clari 👋, a Technical Writer who loves building cool stuff for the web and sharing what I learn along the way. You’ll mostly find me writing about web development and software engineering.

Location:
London
Joined:
Jan 29, 2025

Top 10 Bash Scripts for Every DevOps Engineer

Publish Date: May 12
0 1

In the fast-paced world of DevOps, automation is king. From provisioning servers to deploying applications, Bash scripting helps engineers streamline operations and reduce manual overhead. It’s one of the most essential tools in a DevOps engineer’s toolkit.

This post’ll walk through the Top 10 Bash Scripts Every DevOps Engineer Uses, complete with code snippets, expected output, and a section on Bash vs. Dash—so even beginners can follow along.

💡 New to Bash scripting or want to level up your skills?

Check out the Bash Shell Scripting Training to master scripting fundamentals, automation techniques, and real-world use cases.


🧠 What is Bash (and How is it Different from Dash)?

Bash (Bourne Again Shell) is the default command-line shell for most Linux distributions. It's powerful, flexible, and supports advanced scripting features like arrays, arithmetic, and custom functions.

Dash (Debian Almquist Shell) is a lightweight, POSIX-compliant shell often used as /bin/sh for faster script execution, especially during system boot.

⚙️ Key Differences:

Feature Bash Dash
Speed Slower Faster
Features Rich (arrays, functions) Minimal (POSIX only)
Interactive Use Common Rare
Used for General scripting System scripts (init, boot)
Compatibility Bash-specific scripts may break in Dash Fully POSIX compliant

🔎 Important Tip: Always start your Bash scripts with #!/bin/bash, not #!/bin/sh, if you’re using Bash-specific syntax.


✅ Top 10 Bash Scripts Every DevOps Engineer Uses


1. ✅ Server Health Check Script

#!/bin/bash
echo "Checking system health..."
uptime
df -h
free -m
top -b -n1 | head -15
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

Checking system health...
 11:03:45 up 3 days,  2:11,  2 users,  load average: 0.23, 0.15, 0.12
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   25G   23G  52% /
              total        used        free
Mem:           7989        4121        3868
...
Enter fullscreen mode Exit fullscreen mode

🎯 Use case: Quickly diagnose system health before deploying.


2. ✅ Automated Backup Script

#!/bin/bash
BACKUP_DIR="/backup"
SRC_DIR="/var/www/html"
DATE=$(date +%F)
tar -czvf $BACKUP_DIR/site-backup-$DATE.tar.gz $SRC_DIR
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

/var/www/html/
/var/www/html/index.html
/var/www/html/assets/
Enter fullscreen mode Exit fullscreen mode

📦 Use case: Automates daily website or app backups.


3. ✅ Log Rotation & Cleanup Script

#!/bin/bash
find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

(If logs older than 7 days exist, they are silently deleted.)

🧹 Use case: Prevent disk space bloat from unused logs.


4. ✅ User Management Script

#!/bin/bash
USERNAME=$1
useradd $USERNAME
echo "$USERNAME:ChangeMe123" | chpasswd
passwd --expire $USERNAME
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

Changing password expiry information for user newdev.
passwd: password expiry information changed.
Enter fullscreen mode Exit fullscreen mode

👥 Use case: Adds new users with a default password policy.


5. ✅ Service Monitor & Auto-Restart Script

#!/bin/bash
SERVICE="nginx"
if ! pgrep -x "$SERVICE" > /dev/null; then
  echo "$SERVICE is down. Restarting..."
  systemctl start $SERVICE
else
  echo "$SERVICE is running."
fi
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

nginx is running.
# or
nginx is down. Restarting...
Enter fullscreen mode Exit fullscreen mode

🔄 Use case: Minimizes downtime for critical services.


6. ✅ Deployment Script

#!/bin/bash
cd /var/www/myapp
git pull origin main
chmod -R 755 .
systemctl restart myapp
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

Updating 1a2b3c4..5d6e7f8
Fast-forward
 index.html | 2 +-
Enter fullscreen mode Exit fullscreen mode

🚀 Use case: Deploys the latest code with proper permissions and service restart.


7. ✅ Disk Space Alert Script

#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "Disk usage is ${USAGE}% on root!" | mail -s "Disk Alert" devops@yourdomain.com
fi
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

(Sends an email if disk usage is too high.)

📉 Use case: Avoids system crashes due to full disk.


8. ✅ Database Dump Script

#!/bin/bash
DB_NAME="mydb"
USER="root"
PASS="password"
DATE=$(date +%F)
mysqldump -u$USER -p$PASS $DB_NAME > /backup/db-$DATE.sql
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

Creates a file: /backup/db-2025-05-12.sql

🗃 Use case: Automates MySQL backups for disaster recovery.


9. ✅ System Update Script

#!/bin/bash
apt update && apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Enter fullscreen mode Exit fullscreen mode

🛡 Use case: Keeps the system patched and secure with minimal effort.


10. ✅ Cron Job Health Checker

#!/bin/bash
echo "Listing all cron jobs:"
for user in $(cut -f1 -d: /etc/passwd); do
  crontab -u $user -l 2>/dev/null | grep -v "^#"
done
Enter fullscreen mode Exit fullscreen mode

📤 Expected Output:

0 2 * * * /usr/bin/backup.sh
*/15 * * * * /usr/bin/monitor.sh
Enter fullscreen mode Exit fullscreen mode

📆 Use case: Ensures scheduled tasks are in place and working.


🧩 Final Thoughts

These Bash scripts are the bread and butter of DevOps automation. From monitoring to deployment to backups, a well-written script can save hours of manual work—and prevent critical failures.

💡 Pro Tip: Always test your Bash scripts on a staging environment before using them in production.

Would you like this blog exported as a PDF or published with SEO-optimized formatting for your website?

Comments 1 total

Add comment