25 Real-World Scripting Tasks Every DevOps Engineer Should Know

3 min read

1. Monitor Disk Usage and Alert if Over 80%
#!/bin/bash
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
[ "$usage" -gt 80 ] && echo "Disk usage is above 80%! ($usage%)"
2. Clean /tmp
Directory Older Than 7 Days
#!/bin/bash
find /tmp -type f -mtime +7 -exec rm -f {} \;
3. Monitor Web Service and Alert on Failure
#!/bin/bash
URL="http://yourservice.com"
curl -s --head $URL | grep "200 OK" > /dev/null || echo "Service down!"
4. Rotate Logs and Keep Last 5
#!/bin/bash
cd /var/log/myapp
ls -tp | grep -v '/$' | tail -n +6 | xargs -I {} rm -- {}
5. Backup MySQL DB Daily With Timestamp
#!/bin/bash
mysqldump -u root -pYourPassword dbname > /backup/db_$(date +%F).sql
6. Validate IPs in a Text File
#!/bin/bash
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' ips.txt | while read ip; do
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then echo "Valid: $ip"; fi
done
7. Ping Servers from a File
#!/bin/bash
while read ip; do
ping -c 1 $ip > /dev/null && echo "$ip is up" || echo "$ip is down"
done < servers.txt
8. Email Top CPU-consuming Processes
#!/bin/bash
ps -eo pid,comm,%cpu --sort=-%cpu | head -n 6 | mail -s "Top CPU processes" you@example.com
9. SSH into Multiple Servers and Execute Commands
#!/bin/bash
while read server; do
ssh user@$server 'uptime'
done < servers.txt
10. Archive Application Logs to S3
#!/bin/bash
tar czf app_logs_$(date +%F).tar.gz /var/log/myapp/
aws s3 cp app_logs_$(date +%F).tar.gz s3://your-bucket-name/
11. Restart Service if Down
#!/bin/bash
if ! systemctl is-active --quiet nginx; then
systemctl restart nginx
fi
12. Tail Multiple Logs in Parallel
#!/bin/bash
tail -f /var/log/app1.log /var/log/app2.log
13. Monitor SSL Certificate Expiry
#!/bin/bash
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -enddate
14. Count Failed Logins in Log File
#!/bin/bash
grep "Failed password" /var/log/auth.log | wc -l
15. Report Users with UID > 1000
#!/bin/bash
awk -F: '$3 > 1000 {print $1, $3}' /etc/passwd
16. Find and Kill Zombie Processes
#!/bin/bash
ps aux | awk '$8=="Z" {print $2}' | xargs -r kill -9
17. Compare Two Directories
#!/bin/bash
diff -r dir1/ dir2/
18. Monitor Network Bandwidth in Real-Time
#!/bin/bash
iftop
19. Create Users from CSV
#!/bin/bash
while IFS=, read -r user pass; do
useradd -m $user && echo "$user:$pass" | chpasswd
done < users.csv
20. Alert if Docker Container Not Running
#!/bin/bash
! docker ps | grep -q mycontainer && echo "Container is not running!"
21. Auto-Deploy Static Site Using rsync
#!/bin/bash
rsync -avz /path/to/site/ user@server:/var/www/html/
22. Git Pull + Restart Service
#!/bin/bash
cd /opt/myapp && git pull && systemctl restart myapp
23. Generate Ansible Dynamic Inventory
#!/bin/bash
cat <<EOF
{
"all": {
"hosts": ["192.168.1.10", "192.168.1.11"]
}
}
EOF
24. Validate YAML and JSON Files
#!/bin/bash
yamllint file.yaml
jq empty file.json
25. Parse Jenkins Job Status via API
#!/bin/bash
status=$(curl -s http://jenkins/job/myjob/lastBuild/api/json | jq -r '.result')
[ "$status" != "SUCCESS" ] && echo "Build failed!"
1
Subscribe to my newsletter
Read articles from Amit singh deora directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Amit singh deora
Amit singh deora
DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab