A shell script is a text file containing a series of commands that are executed by a Unix/Linux shell (command-line interpreter). It automates repetitive tasks, combines multiple commands, and can include programming constructs like loops, conditionals, and functions.
Key Features of Shell Scripting:
- Automation — Runs commands without manual input.
- Batch Processing — Executes multiple commands sequentially.
- Scripting Language — Supports variables, loops, and conditionals.
- Reusability — Scripts can be saved and reused.
- Customization — Can take user input and generate dynamic output.
1. Backup Script
mkdir source
touch /source/1.txt /source/2.txt # or cd /source/ && touch 1.txt 2.txt
mkdir destination
#! /bin/bash
SOURCE="/home/ubuntu/source"
DESTINATION="/home/ubuntu/destination"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
#Create backup directory and copy files
mkdir -p $DESTINATION/$DATE
cp -r $SOURCE $DESTINATION/$DATE
echo "Backup completed on $DATE"
*or*
cp -r "$SOURCE" "$DESTINATION/$DATE" && echo "Backup succeeded on $DATE" || echo "Backup failed on $DATE" >&2
sudo chmod +x backup.sh
./backup.sh
Explanation
• SOURCE: The directory to be backed up.
• DESTINATION: The directory where the backup will be stored.
• DATE: Captures the current date and time to create a unique backup folder.
• mkdir -p $DESTINATION/$DATE: Creates the backup directory if it does not exist.
• cp -r $SOURCE $DESTINATION/$DATE: Copies the contents of the source directory
to the backup directory.
• echo "Backup completed on $DATE": Outputs a message indicating the completion
of the backup.
Scheduling with Cron
To run the backup script at regular intervals, use crontab -e to edit the crontab file
and add:
* * * * * /path/to/backup_script.sh
This example runs the script every minute. Adjust the schedule as needed.
Remove or Disable the Cron Job
crontab -e
Method 1. Delete the line containing your backup script and save
Method 2. # * * * * * /path/to/backup_script.sh # add # and save
Method 3. crontab -r # Delete your entire crontab
Method 4: Stop the Cron Service
sudo systemctl stop cron # Ubuntu/Debian
sudo systemctl stop crond # CentOS/RHEL
2. Disk Usage Check Script
#!/bin/bash
THRESHOLD=80
# Check disk usage and print a warning if usage is above the threshold
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
Explanation
• THRESHOLD: Sets the disk usage percentage threshold.
• df -H: Lists disk usage in human-readable format.
• grep -vE '^Filesystem|tmpfs|cdrom': Filters out unnecessary lines.
• awk '{ print $5 " " $1 }': Extracts the usage percentage and partition name.
• while read output: Iterates over each line of the filtered output.
• usage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1): Extracts the usage
percentage.
• partition=$(echo $output | awk '{ print $2 }'): Extracts the partition name.
• if [ $usage -ge $THRESHOLD ]; then: Checks if the usage exceeds the threshold.
• echo "Warning: Disk usage on 𝑝𝑎𝑟𝑡𝑖𝑡𝑖𝑜𝑛𝑖𝑠𝑎𝑡{usage}%": Prints a warning message
3. Service Health Check Script
#!/bin/bash
SERVICE="nginx"
# Check if the service is running, if not, start it
if systemctl is-active --quiet $SERVICE; then
echo "$SERVICE is running"
else
echo "$SERVICE is not running"
systemctl start $SERVICE
fi
Explanation
• SERVICE: The name of the service to check.
• systemctl is-active --quiet $SERVICE: Checks if the service is running.
• echo "$SERVICE is running": Prints a message if the service is running.
• systemctl start $SERVICE: Starts the service if it is not running.
sudo apt install nginx
sudo systemctl status nginx
./service-health.sh
sudo systemctl stop nginx
4. Network Connectivity Check Script
touch output.txt
#!/bin/bash
HOST="google.com"
# Output file
OUTPUT_FILE="/home/ubuntu/output.txt"
# Check if the host is reachable
if ping -c 1 $HOST &> /dev/null
then
echo "$HOST is reachable" >> $OUTPUT_FILE
else
echo "$HOST is not reachable" >> $OUTPUT_FILE
fi
Explanation
• HOST: The hostname to check.
• OUTPUT_FILE: The file to write the output to.
• ping -c 1 $HOST &> /dev/null: Pings the host once, suppressing output.
• echo "$HOST is reachable" >> $OUTPUT_FILE: Writes to the output file if the host
is reachable.• echo "$HOST is not reachable" >> $OUTPUT_FILE: Writes to the output file if the
host is not reachable.
5. Database Backup Script
Installation
Install MySQL:
sudo apt install mysql-server -y
Set up MySQL password:
sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY
'root';
FLUSH PRIVILEGES;
> show databases;
create database mydatabase;
exit
mkdir /home/ubuntu/db_backup # for this /path/to/backup
Method 1.
#!/bin/bash
DB_NAME="mydatabase"
BACKUP_DIR="/path/to/backup"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
# Perform a database backup and save it to the backup directory
mysqldump -u root -p $DB_NAME > $BACKUP_DIR/$DB_NAME-$DATE.sql
echo "Database backup completed: $BACKUP_DIR/$DB_NAME-$DATE.sql"
Method 2. auto fill password
#!/bin/bash
DB_NAME="mydatabase"
BACKUP_DIR="/path/to/backup"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
DB_PASSWORD="root" # Added password variable
# Perform the backup (password provided automatically)
mysqldump -u root -p"$DB_PASSWORD" "$DB_NAME" > "$BACKUP_DIR/$DB_NAME-$DATE.sql"
echo "Database backup completed: $BACKUP_DIR/$DB_NAME-$DATE.sql"
method 3. For production and Security :
vim ~/.my.cnf
chmod 600 ~/.my.cnf
[mysqldump]
user=root
password=yourpassword
#!/bin/bash
DB_NAME="mydatabase"
BACKUP_DIR="/path/to/backup"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
mysqldump "$DB_NAME" > "$BACKUP_DIR/$DB_NAME-$DATE.sql"
echo "Backup completed: $BACKUP_DIR/$DB_NAME-$DATE.sql"
6. System Uptime Check Script
#!/bin/bash
# Print the system uptime
uptime -p
Explanation
• uptime -p: Prints the system uptime in a human-readable format.
7. Listening Ports Script
Installation
Install net-tools:
sudo apt install net-tools
#!/bin/bash
# List all listening ports and the associated services
netstat -tuln | grep LISTEN
Explanation
• netstat -tuln: Lists all TCP and UDP listening ports.
• grep LISTEN: Filters the output to show only listening ports.
8. Automatic Package Updates Script
#!/bin/bash
# Update system packages and clean up unnecessary packages
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean
echo "System packages updated and cleaned up"
Explanation
• apt-get update: Updates the package list.
• apt-get upgrade -y: Upgrades all installed packages.
• apt-get autoremove -y: Removes unnecessary packages.
• apt-get clean: Cleans up the package cache.
• echo "System packages updated and cleaned up": Outputs a message indicating
the completion of the update and cleanup.
9. HTTP Response Times Script
#!/bin/bash
URLS=("https://www.google.com/" "https://www.linkedin.com/")
# Check HTTP response times for multiple URLs
for URL in "${URLS[@]}"; do
RESPONSE_TIME=$(curl -o /dev/null -s -w '%{time_total}\n' $URL)
echo "Response time for $URL: $RESPONSE_TIME seconds"
done
Explanation
• URLS: An array of URLs to check.
• for URL in "${URLS[@]}": Iterates over each URL.
• curl -o /dev/null -s -w '%{time_total}\n' $URL: Uses curl to fetch the URL and
measure the total response time.
• echo "Response time for $URL: $RESPONSE_TIME seconds": Prints the response
time for each URL.
10. Monitor System Processes and Memory Usage Script
#!/bin/bash
# Monitor system processes and their memory usage
ps aux --sort=-%mem | head -n 10
Explanation
• ps aux: Lists all running processes.
• --sort=-%mem: Sorts the processes by memory usage in descending order.
• head -n 10: Displays the top 10 processes by memory usage.
11. System Monitoring Script:
#!/bin/bash
threshold=90
# Monitor CPU usage and trigger alert if threshold exceeded
# Method 1: Using vmstat (most reliable)
cpu_usage=$(vmstat 1 2 | tail -1 | awk '{print 100-$15}' | cut -d. -f1)
# Alternative method using top (if vmstat doesn't work)
# cpu_usage=$(top -bn1 | grep -i "cpu(s)" | awk '{gsub(/%us,|%user,|%/, ""); for(i=1;i<=NF;i++) if($i ~ /^[0-9]+\.?[0-9]*$/) {print $i; exit}}')
# Debug: Show what we captured
echo "Current CPU usage: $cpu_usage%"
if [ ! -z "$cpu_usage" ] && [ "$cpu_usage" -gt "$threshold" ]; then
echo "HIGH CPU ALERT: Usage is $cpu_usage% (threshold: $threshold%)"
# Add alert/notification logic here
# Example: mail -s "High CPU Alert" admin@example.com < /dev/null
# Example: logger "High CPU usage detected: $cpu_usage%"
else
echo "CPU usage normal: $cpu_usage%"
fi
use stress cmd for better results
Top comments (0)