Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’3 min read
πŸ“š 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 up

  • DESTINATION β†’ Backup storage path

  • DATE β†’ Timestamped folder

  • mkdir -p β†’ Creates directory if missing

  • cp -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!

More from this blog

D

DevSecOps - Zero To Hero

19 posts