USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Practical Automation Examples

Lesson 15/32 | Study Time: 20 Min

Automation is a key benefit of shell scripting and command-line proficiency in Linux. By automating repetitive, time-consuming tasks, developers and system administrators save time, reduce errors, and streamline operational workflows.

Practical automation examples illustrate how everyday tasks such as deployments, backups, log rotations, and resource monitoring can be scripted effectively. 

Automated Deployment Script

Automating application deployment minimizes manual errors and ensures consistent setups. A deployment script can stop running services, pull updated code, install dependencies, and restart services.


Example:

bash
#!/bin/bash
echo "Starting deployment..."
service myapp stop
cd /var/www/myapp || exit
git pull origin main
npm install
service myapp start
echo "Deployment completed."


This script stops the app, updates the codebase, installs dependencies, and restarts the app, enabling reliable continuous deployment.

Backup Automation

Automated backups protect data by scheduling regular copies without manual intervention.


Example:

bash
#!/bin/bash
backup_dir="/backup/$(date +%Y-%m-%d)"
mkdir -p "$backup_dir"
tar -czf "$backup_dir/home_backup.tar.gz" /home/user/
echo "Backup completed on $(date)" >> /var/log/backup.log


This script creates a dated backup directory and archives the user’s home directory, appending a log entry for auditing.

Log Rotation Automation

Logs can grow large and unwieldy. Automating log rotation keeps them manageable and prevents disk space issues.


Example:

bash
#!/bin/bash
log_file="/var/log/myapp.log"
max_size=10485760 # 10MB in bytes
file_size=$(stat -c%s "$log_file")

if [ "$file_size" -ge "$max_size" ]; then
mv "$log_file" "${log_file}.$(date +%F-%T)"
touch "$log_file"
systemctl restart myapp.service
echo "Log rotated on $(date)" >> /var/log/myapp.log
fi


The script checks if the log file exceeds 10MB, archives it with a timestamp, creates a new log, and restarts the service.

Disk Space Monitoring

Scripts can alert administrators when disk usage exceeds thresholds, enabling proactive maintenance.


Example:

bash
#!/bin/bash
threshold=80
usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')

if [ "$usage" -ge "$threshold" ]; then
echo "Disk space is critically high: $usage%" | mail -s "Disk Alert" admin@example.com
fi


This script monitors root filesystem usage and emails an alert if usage surpasses 80%.

Git Workflow Helpers

Scripts can simplify frequent Git tasks.


Example: Automate adding all, commit with a message, and pushing changes:

bash
#!/bin/bash
git add .
git commit -m "$1"
git push origin main


Running ./gitpush.sh "Commit message" stages changes, commits with the message, and pushes to main remotely.

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.