devConsulting.blog

10 Practical Shell Scripts to Automate Server Management

Server management often involves repetitive tasks -- monitoring disk space, checking running processes, managing backups, and restarting services. With shell scripting, you can automate these routine jobs, save time, and reduce human error. In this blog, we will go through 10 practical shell scripts every system administrator or DevOps engineer should have in their toolkit.
  1. Check Disk Usage and Alert When Full

  2. This script checks if your disk usage exceeds a threshold (e.g., 80%) and sends an alert.
    #!/bin/bash
    threshold=80
    used=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

    if [ "$used" -ge "$threshold" ]; then
    echo "Disk usage is above $threshold%! Used: $used%"
    # Send mail or Slack alert here
    fi
    Use Case: Monitor servers for low disk space to prevent crashes.
  3. Monitor System Uptime

  4. Simple script to log system uptime daily.
    #!/bin/bash
    echo "$(date): $(uptime -p)" >> /var/log/system_uptime.log
    Use Case: Keep a record of uptime for performance analysis or reporting.
  5. Backup a Directory Automatically

  6. Automate periodic backups of important folders.
    #!/bin/bash
    src="/var/www/html"
    dest="/backup/html_$(date +%F).tar.gz"

    tar -czf "$dest" "$src"
    echo "Backup created at $dest"
    Use Case: Automatically back up web or app files daily.
  7. Restart a Service if It Stops

  8. Monitor and restart critical services like Apache or MySQL.
    #!/bin/bash
    service="apache2"
    if ! pgrep -x "$service" >/dev/null; then
    echo "$service is down! Restarting..."
    systemctl restart "$service"
    fi
    Use Case: Ensure high availability of web or database services.
  9. Clean Temporary Files

  10. Remove old log and temp files to free up space.
    #!/bin/bash
    find /tmp -type f -mtime +7 -exec rm -f {} \;
    echo "Old temp files deleted."
    Use Case: Keeps your system clean and prevents clutter.
  11. Check Active Connections

  12. Display top IPs connected to your server.
    #!/bin/bash
    netstat -tn 2>/dev/null | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
    Use Case: Identify potential attacks or heavy users.
  13. CPU and Memory Usage Report

  14. Quickly check CPU and RAM stats.
    #!/bin/bash
    echo "===== CPU USAGE ====="
    top -bn1 | grep "Cpu(s)"
    echo "===== MEMORY USAGE ====="
    free -h
    Use Case: Useful for periodic performance snapshots.
  15. Automate System Updates

  16. Keep your server secure with regular updates.
    #!/bin/bash
    apt update && apt upgrade -y
    echo "System updated on $(date)" >> /var/log/update.log
    Use Case: Schedule this in cron for weekly updates.
  17. Monitor User Logins

  18. Track who logs into your server.
    #!/bin/bash
    last -a | head -n 10
    Use Case: Detect unauthorized or suspicious access.
  19. Auto-Reboot After Kernel Update

  20. Reboot only if required after updates.
    #!/bin/bash
    if [ -f /var/run/reboot-required ]; then
    echo "Reboot required. Rebooting now..."
    reboot
    fi
    Use Case: Avoid downtime after kernel updates by automating restarts safely.

Wrapping Up

These 10 shell scripts cover common server management tasks:
  • System monitoring
  • Backups
  • Performance reporting
  • Automated maintenance
You can schedule these scripts using cron jobs to run daily, weekly, or monthly — making your servers self-managing and more reliable.
SS
Samarth Srivastava

Related Posts

How to Add a Spinner, Tick Marks, and Logging in Your Shell Script
When you run a build script, plain echo messages often feel dull - you don't know if something is still running or stuck. Adding a spinner animation a...  Read Here →
Understanding Cybersecurity Injections: Examples and Mitigations
Injection attacks are among the most common and dangerous web application vulnerabilities, injection flaws (like SQL Injection, Command Injection, and...  Read Here →
How to Create an Angular Library and Publish it as NPM Package
When building large-scale Angular applications, you may develop reusable components, directives, or services that could benefit other projects. Instea...  Read Here →
How to Create an NPM Package: A Step-by-Step Guide
Creating and publishing your own NPM (Node Package Manager) package can be a great way to share useful code, collaborate with the open-source communit...  Read Here →
Understanding AI Agents and Using Them to Generate Code
AI agents are autonomous or semi-autonomous systems designed to interact with their environment, make decisions, and perform tasks to achieve goals. I...  Read Here →