Operations 5 min read

10 Powerful Shell Scripts to Automate System Tasks

This article presents ten advanced Bash shell scripts that automate common system administration tasks such as directory backup, resource monitoring, automatic updates, database dumping, FTP uploads, log keyword alerts, report generation, service recovery, temporary file cleanup, and network status checking, each ready for customization.

Architect's Must-Have
Architect's Must-Have
Architect's Must-Have
10 Powerful Shell Scripts to Automate System Tasks

Below are ten advanced shell script examples, each solving a different problem or performing a distinct task.

1. Backup Directory

#!/bin/bash

SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")

if [ ! -d "$BACKUP_DIR" ]; then
  mkdir -p "$BACKUP_DIR"
fi

tar -czf "$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" -C "$SOURCE_DIR" .
echo "Backup of $SOURCE_DIR completed at $BACKUP_DIR/backup_$TIMESTAMP.tar.gz"

2. Monitor System Resources

#!/bin/bash

LOG_FILE="/var/log/system_monitor.log"
INTERVAL=5

while true; do
  DATE=$(date +"%Y-%m-%d %H:%M:%S")
  CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
  MEM=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
  DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}')
  echo "$DATE CPU: $CPU, Memory: $MEM, Disk: $DISK" >> $LOG_FILE
  sleep $INTERVAL
done

3. Automatic System Update

#!/bin/bash

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y autoremove
sudo apt-get -y autoclean
echo "System update and cleanup completed."

4. Database Backup

#!/bin/bash

DB_NAME="your_database"
DB_USER="your_username"
DB_PASS="your_password"
BACKUP_DIR="/path/to/backup"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")

mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > "$BACKUP_DIR/db_backup_$TIMESTAMP.sql.gz"
echo "Database backup completed at $BACKUP_DIR/db_backup_$TIMESTAMP.sql.gz"

5. Automatic FTP Upload

#!/bin/bash

FTP_SERVER="ftp.example.com"
FTP_USER="your_username"
FTP_PASS="your_password"
LOCAL_FILE="/path/to/local/file"
REMOTE_DIR="/path/to/remote/dir"

ftp -inv $FTP_SERVER <<EOF
user $FTP_USER $FTP_PASS
cd $REMOTE_DIR
put $LOCAL_FILE
bye
EOF

echo "File uploaded to $FTP_SERVER:$REMOTE_DIR"

6. Monitor Log File for Keywords

#!/bin/bash

LOG_FILE="/var/log/syslog"
KEYWORD="ERROR"
EMAIL="[email protected]"

tail -Fn0 $LOG_FILE | while read line; do
  echo "$line" | grep "$KEYWORD" &>/dev/null
  if [ $? = 0 ]; then
    echo "$line" | mail -s "Error found in log file" $EMAIL
  fi
done

7. Automatic Report Generation

#!/bin/bash

REPORT_DIR="/path/to/report"
REPORT_FILE="$REPORT_DIR/report_$(date +"%Y%m%d").txt"

if [ ! -d "$REPORT_DIR" ]; then
  mkdir -p "$REPORT_DIR"
fi

echo "Report generated on $(date)" > $REPORT_FILE

echo "==========================" >> $REPORT_FILE
df -h >> $REPORT_FILE
echo "" >> $REPORT_FILE
free -m >> $REPORT_FILE
echo "" >> $REPORT_FILE
uptime >> $REPORT_FILE

echo "Report saved at $REPORT_FILE"

8. Restart Crashed Service

#!/bin/bash

SERVICE_NAME="apache2"
if ! pgrep -x "$SERVICE_NAME" > /dev/null; then
  echo "$SERVICE_NAME is not running. Starting $SERVICE_NAME."
  systemctl start $SERVICE_NAME
else
  echo "$SERVICE_NAME is running."
fi

9. Clean Up Temporary Files

#!/bin/bash

TEMP_DIR="/path/to/temp"
DAYS_OLD=7

find $TEMP_DIR -type f -mtime +$DAYS_OLD -exec rm -f {} \;
echo "Old temporary files deleted."

10. Network Connection Status Monitoring

#!/bin/bash

HOST="8.8.8.8"
LOG_FILE="/var/log/network_status.log"
INTERVAL=10

while true; do
  DATE=$(date +"%Y-%m-%d %H:%M:%S")
  if ping -c 1 $HOST > /dev/null; then
    echo "$DATE Network is up" >> $LOG_FILE
  else
    echo "$DATE Network is down" >> $LOG_FILE
  fi
  sleep $INTERVAL
done

Each script can be adjusted as needed to fit specific environments and requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

bashshell scriptingsystem automation
Architect's Must-Have
Written by

Architect's Must-Have

Professional architects sharing high‑quality architecture insights. Covers high‑availability, high‑performance, high‑stability designs, big data, machine learning, Java, system, distributed and AI architectures, plus internet‑driven architectural adjustments and large‑scale practice. Open to idea‑driven, sharing architects for exchange and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.