Shell Scripts Every DevOps Engineer Should Know

Shell Scripts Every DevOps Engineer Should Know

In DevOps jobs, engineers regularly write shell scripts to check system performance and automate routine operational tasks. However, DevOps engineers do not run these scripts manually by logging into servers every day. Instead, they use cron jobs to execute scripts automatically at scheduled intervals. This approach ensures reliability, consistency, and zero human dependency.

What Is a Cron Job?

A cron job is a Linux-based scheduling utility that allows commands or scripts to run automatically at fixed times, dates, or recurring intervals. It is mainly used for repetitive DevOps tasks such as:

  • System updates and patching
  • Database and file backups
  • Log rotation and cleanup
  • Health checks and monitoring
  • Service restarts

Cron jobs help DevOps teams maintain systems without manual intervention, even when engineers are offline.


How to Configure a Cron Job?

To create or edit cron jobs for the current user, we use the following command:

crontab -e

Cron jobs follow a specific syntax that has five star fields:

* * * * *

Cron Time Format Explanation

┌──────── Minute (0 - 59)
│ ┌────── Hour (0 - 23)
│ │ ┌──── Day of Month (1 - 31)
│ │ │ ┌── Month (1 - 12)
│ │ │ │ ┌─ Day of Week (0 - 7) (0 or 7 = Sunday)
│ │ │ │ │
* * * * *

Run Script on the 1st of Every Month at 12:00 AM

0 0 1 * * /home/venkat/system-update.sh >> /var/log/system-update.log 2>&1

Explanation:

  • 0 0 1 – Runs the script on the 1st of every month at 12:00 AM
  • /home/venkat/system-update.sh – This is the script path
  • /var/log/system-update.log – This file stores the script output, and >> is used to avoid overwriting the file
  • 2>&1 – Stores both standard output and error output

Expected Output:

Starting system update at Mon Feb 1 00:00:01 IST 2026
System update completed at Mon Feb 1 00:03:22 IST 2026

Sample Shell Script for System Update

Below is a simple shell script used for system upgrades on Ubuntu-based servers.

#!/bin/bash
echo "Starting system update at $(date)"
sudo apt update -y
sudo apt upgrade -y
echo "System update completed at $(date)"

First, Give Execution Permission

Before scheduling the script, you must give it execution permission.

chmod +x /home/venkat/system-update.sh

Check if a Service Is Running or Not

Using this script, we can check whether a service is running or not. The service name is passed dynamically as a command-line argument.

#!/bin/bash
SERVICE=$1
if systemctl is-active --quiet "$SERVICE"; then
echo "$SERVICE is running"
else
echo "$SERVICE is not running"
systemctl start "$SERVICE"
fi

Explanation

  • $1 – Represents the first command-line argument passed to the script (the service name).
  • systemctl is-active –quiet – Checks whether the service is running.
    • If the service is running, it returns exit code 0 (success).
    • If the service is not running, it returns a non-zero exit code (failure).

Run the Script

./service_check.sh nginx

Expected Output

nginx is running

Automated Backup Script in Linux

This script is used to automatically back up application log files from the system. Regular backups help prevent data loss caused by accidental deletion, system failures, or application issues.

#!/bin/bash
SOURCE="/var/log"
DEST="/backup"
DATE=$(date +%F)
ARCHIVE="logs_backup_$DATE.tar.gz"
mkdir -p "$DEST"
tar -czf "$DEST/$ARCHIVE" "$SOURCE"
if [ $? -eq 0 ]; then
echo "Backup completed successfully on $DATE"
else
echo "Backup failed on $DATE"
fi

Explanation

  • DATE will store the current date in this  YYYY-MM-DD format.
  • mkdir -p creates the backup directory if it does not already exist.
  • tar -czf creates a compressed archive of the source directory.
  • $? stores the exit status of the last executed command.
  • An exit code of 0 means the backup was successful; any other value indicates failure.

Expected Output:

Backup completed successfully on 2026-01-13

Log Rotation and Compression

For long-running applications, log files continuously grow over time. If logs are not rotated, they can become very large, difficult to analyze, and may even consume critical disk space. Log rotation helps by archiving old logs and compressing them, which improves system performance, optimizes disk usage, and simplifies log management.

#!/bin/bash
LOG_DIR="/var/log/myapp"
ARCHIVE_DIR="/var/log/myapp/archive"
DATE=$(date +%F)
Create the archive directory if it does not exist
mkdir -p "$ARCHIVE_DIR"
Rotate and compress each .log file
for log in "$LOG_DIR"/*.log
do
if [ -f "$log" ]; then
mv "$log" "$ARCHIVE_DIR/$(basename "$log")$DATE"
gzip "$ARCHIVE_DIR/$(basename "$log")$DATE"
fi
done

Explanation

  • for log in “$LOG_DIR”/*.log – Loops through all .log files in the log directory.
  • mv – Moves the log file to the archive directory and appends the current date (for example, app.log_2026-01-13). The basename command is used because $log contains the full file path, and we only want the file name.
  • gzip – Compresses the rotated log file to save disk space (for example, app.log_2026-01-13.gz).
  • if [ -f “$log” ] – Ensures the loop runs only when the log file exists.

Expected Output:

/var/log/myapp/archive/app.log_2026-01-13.gz

System Monitoring Script (80% Threshold)

This script helps DevOps engineers understand how much CPU, memory (RAM), and disk resources are currently being used on a Linux system. If the usage of any resource crosses the defined threshold (80% in this case), the script displays an alert so that necessary actions can be taken in advance.

#!/bin/bash
THRESHOLD=80
CPU usage
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print int(100 - $8)}')
Memory usage
MEM=$(free | awk '/Mem/ {printf "%d", $3/$2*100}')
Disk usage
DISK=$(df / | awk 'NR==2 {print int($5)}')
echo "CPU Usage    : $CPU%"
echo "Memory Usage : $MEM%"
echo "Disk Usage   : $DISK%"
echo "---------------------------"
Alerts
if [ $CPU -ge $THRESHOLD ]; then
echo "ALERT: CPU usage exceeded $THRESHOLD%"
fi
if [ $MEM -ge $THRESHOLD ]; then
echo "ALERT: Memory usage exceeded $THRESHOLD%"
fi
if [ $DISK -ge $THRESHOLD ]; then
echo "ALERT: Disk usage exceeded $THRESHOLD%"
fi

Explanation

  • In this script, the threshold is set to 80%. You can change this value based on your system requirements.
  • top -bn1 provides CPU statistics. The script subtracts the idle CPU percentage ($8) from 100 to calculate the actual CPU usage.
  • free displays memory usage details. From the Mem row: $2 represents total memory and $3 represents used memory. The percentage is calculated using the formula: (used / total) * 100.
  • df / checks the disk usage of the root directory. The value from the 5th – $5  column is extracted and converted into a numeric value.

Expected Output:

CPU Usage    : 92%
Memory Usage : 61%
Disk Usage   : 58%
---------------------------
ALERT: CPU usage exceeded 80%

Check if the Required Tools Are Installed

In DevOps, engineers work with multiple tools such as Git, Docker, and kubectl. Manually checking whether each tool is installed on a system can be time-consuming. This script automates the process by verifying if the required tools are available on the system.

#!/bin/bash
TOOLS=("git" "docker" "kubectl")
for tool in "${TOOLS[@]}"
do
if command -v "$tool" &>/dev/null; then
echo "$tool is installed"
else
echo "$tool is NOT installed"
fi
done

Explanation

  • TOOLS is an array that contains the names of tools to check.
  • A for loop iterates through each tool in the array.
  • command -v checks whether the command exists in the system.
  • &>/dev/null suppresses both standard output and error messages.
  • If the command exists, the exit status is 0, which means the tool is installed.

Expected Output:

git is installed
docker is installed
kubectl is NOT installed

Check Multiple Services at Once

This script helps DevOps engineers check whether multiple services are running at the same time. If any service is stopped, necessary actions can be taken immediately.

#!/bin/bash
SERVICES=("nginx" "docker" "ssh")
for service in "${SERVICES[@]}"
do
systemctl is-active --quiet "$service" && echo "$service running" || echo "$service stopped"
done

Explanation

  • systemctl is-active --quiet checks the service status without displaying output.
  • If the service is active, the command exits with status 0.
  • && executes the success message when the exit code is 0.
  • || executes the failure message when the exit code is non-zero.
  • This script avoids using explicit if-else conditions.

Expected Output:

nginx running
docker running
ssh running

Find the Top 10 Largest Files

#!/bin/bash
find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -10 > largest_files.txt

Explanation

  • find / -type f searches for all files starting from the root directory.
  • du -h calculates file sizes in human-readable format.
  • 2>/dev/null suppresses permission-denied errors.
  • sort -rh sorts files by size in descending order.
  • head -10 extracts the top 10 largest files.
  • The output is stored in largest_files.txt.

Expected Output:

6.1G /var/log/application.log
3.8G /home/backups/db_backup.sql
2.9G /opt/data/archive.tar

Check URLs and Send Alert if Down

#!/bin/bash
URLS=("https://example.com")
for url in "${URLS[@]}"
do
if ! curl -s --head "$url" | grep "200 OK" > /dev/null; then
echo "$url is DOWN" | mail -s "Website Alert" admin@example.com
fi
done

Explanation

  • curl -s --head sends a silent HTTP HEAD request.
  • grep "200 OK" checks for a successful HTTP response.
  • ! negates the condition and triggers when the site is down.
  • If the site is unreachable, an email alert is sent to the administrator.

Expected Output

Email alert triggered when a URL is down

User Management – Create User If Not Exists

#!/bin/bash
USERNAME="devuser"
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists"
else
useradd "$USERNAME"
echo "User $USERNAME created successfully"
fi

Explanation

  • USERNAME stores the username to be checked.
  • id "$USERNAME" verifies whether the user exists.
  • &>/dev/null suppresses command output.
  • If the user does not exist, the script creates it using useradd.

Expected Output:

User devuser created successfully

Be the first to comment

Leave a Reply

Your email address will not be published.


*