π 10 Real-World Shell Scripts Used by DevOps Engineers (With Code)

When I started learning Linux & Shell Scripting, I realized something important π
Most real production problems are solved using simple Bash scripts, not complex tools.
In this article, Iβm sharing 10 real-world shell scripts that are actively used in companies and that I discovered while learning DevOps.
If youβre preparing for a DevOps / Cloud / SRE role, this will help you understand what actually matters on the job.
π§ Why Shell Scripting Is Critical for DevOps
Shell scripts help DevOps engineers:
βοΈ Automate repetitive tasks
π Monitor system health
π‘οΈ Improve reliability & uptime
π Reduce manual errors
π Save time in production
π 1. Backup Automation Script
π Use case: Take timestamped backups of important directories
π‘οΈ Used for: Disaster recovery & audits
#!/bin/bash
SOURCE="/home/ubuntu/aditya"
DESTINATION="/home/ubuntu/jaiswal"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
mkdir -p "$DESTINATION/$DATE"
cp -r "$SOURCE" "$DESTINATION/$DATE"
echo "Backup completed on $DATE"
π§ Explanation:
SOURCEβ Directory to back upDESTINATIONβ Backup storage pathDATEβ Timestamped foldermkdir -pβ Creates directory if missingcp -rβ Recursively copies files
π Cron Automation Example:
0 1 * * * /path/to/backup.sh
π½ 2. Disk Usage Monitoring Script
π Use case: Prevent disk-full outages
#!/bin/bash
THRESHOLD=80
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' |
while read output; do
usage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
partition=$(echo $output | awk '{ print $2 }')
if [ "$usage" -ge "$THRESHOLD" ]; then
echo "β οΈ Warning: Disk usage on $partition is at ${usage}%"
fi
done
π§ Why it matters:
Disk space issues are one of the most common production failures.
π οΈ 3. Service Health Check (Self-Healing)
π Use case: Auto-restart failed services
#!/bin/bash
SERVICE="nginx"
if systemctl is-active --quiet "$SERVICE"; then
echo "$SERVICE is running"
else
echo "$SERVICE is down. Restarting..."
systemctl start "$SERVICE"
fi
π§ Production value:
Improves uptime without manual intervention.
π 4. Network Connectivity Check
π Use case: Check internet or host availability
#!/bin/bash
HOST="google.com"
OUTPUT_FILE="/home/ubuntu/output.txt"
if ping -c 1 "$HOST" &> /dev/null; then
echo "$HOST is reachable" >> "$OUTPUT_FILE"
else
echo "$HOST is not reachable" >> "$OUTPUT_FILE"
fi
π’οΈ 5. MySQL Database Backup Script
π Use case: Secure database backups
#!/bin/bash
DB_NAME="mydatabase"
BACKUP_DIR="/path/to/backup"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
mysqldump -u root -p "$DB_NAME" > "$BACKUP_DIR/$DB_NAME-$DATE.sql"
echo "Database backup completed"
π§ Tip:
Always automate DB backups using cron in production.
β±οΈ 6. System Uptime Script
#!/bin/bash
uptime -p
π Helps track server stability and reboots.
π‘ 7. Listening Ports Checker
#!/bin/bash
netstat -tuln | grep LISTEN
π Used for debugging network & service issues.
β»οΈ 8. Automatic Package Updates
#!/bin/bash
apt-get update &&
apt-get upgrade -y &&
apt-get autoremove -y &&
apt-get clean
echo "System updated successfully"
π Keeps servers secure and clean.
π 9. HTTP Response Time Monitoring
#!/bin/bash
URLS=("https://www.devopsshack.com/" "https://www.linkedin.com/")
for URL in "${URLS[@]}"; do
RESPONSE_TIME=$(curl -o /dev/null -s -w '%{time_total}\n' "$URL")
echo "Response time for $URL: ${RESPONSE_TIME}s"
done
π Used for performance & uptime monitoring.
π§ 10. Top Memory-Consuming Processes
#!/bin/bash
ps aux --sort=-%mem | head -n 10
π Helps identify memory bottlenecks quickly.
π― How This Helps in DevOps Jobs
These scripts show:
β Linux fundamentals
β Automation skills
β Production mindset
β Monitoring & reliability knowledge
π Source Code & Learning Notes
π GitHub Repository: https://github.com/Jysh06/10-Real-Time-Shell-Scripts
βοΈ Hashnode Blog: Youβre reading it π
π Final Thoughts
If youβre learning DevOps, start with:
Linux
Shell scripting
Real production problems
Iβm documenting my journey learning in public to help me and others transitioning into DevOps.
β If this helped you, consider sharing or starring the repo!




