0% found this document useful (0 votes)
31 views

Shell Scripting Real time Automations

Uploaded by

nm.nielit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Shell Scripting Real time Automations

Uploaded by

nm.nielit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Top Shell Scripts Asked in Interviews

Backup Script
Task: Write a script to back up a directory (e.g., /var/log/) to another location, compress it, and add a

timestamp to the filename.

#!/bin/bash

src_dir="/var/log"

backup_dir="/backup/logs"

filename="log_backup_$(date +%Y%m%d_%H%M%S).tar.gz"

tar -czf $backup_dir/$filename $src_dir

echo "Backup completed: $filename"

Disk Usage Alert Script


Task: Monitor disk usage and send an alert if usage exceeds a defined threshold.

#!/bin/bash

threshold=80

usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ $usage -gt $threshold ]; then

echo "Disk usage at $usage%, sending alert!"

# Uncomment below to send email (requires mail setup)

# echo "Disk usage alert" | mail -s "Disk Usage Warning" [email protected]

fi

Service Monitoring Script


Task: Check if a service (e.g., Apache) is running, and restart it if it is down.
#!/bin/bash

service="apache2"

if ! systemctl is-active --quiet $service; then

echo "$service is down, restarting..."

systemctl start $service

else

echo "$service is running."

fi

User Creation Script


Task: Create users from a file containing usernames.

#!/bin/bash

input="users.txt"

while IFS= read -r user

do

useradd -m $user

echo "User $user created."

done < "$input"

Log Rotation Script


Task: Rotate logs by compressing logs older than 7 days.

#!/bin/bash

log_dir="/var/log/myapp"

find $log_dir -type f -mtime +7 -exec tar -czf {}.tar.gz {} \; -exec rm {} \;

echo "Logs older than 7 days have been archived."


File Archiving Script
Task: Archive files older than 7 days.

#!/bin/bash

src_dir="/data/files"

archive_dir="/archive"

find $src_dir -type f -mtime +7 -exec mv {} $archive_dir \;

echo "Archived files older than 7 days."

Database Backup Script


Task: Back up a MySQL database.

#!/bin/bash

db_name="mydatabase"

backup_dir="/backup/db"

filename="db_backup_$(date +%Y%m%d_%H%M%S).sql.gz"

mysqldump -u root -p $db_name | gzip > $backup_dir/$filename

echo "Database backup completed: $filename"

Log File Parsing Script


Task: Extract specific keywords from a log file.

#!/bin/bash

logfile="/var/log/syslog"

keyword="ERROR"

grep $keyword $logfile > error_log.txt

echo "All ERROR entries saved to error_log.txt."


File Processing Script (Processing 100 lines at a time)
Task: Process a large file in chunks.

#!/bin/bash

filename="largefile.txt"

chunk_size=100

split -l $chunk_size $filename part_

for file in part_*

do

echo "Processing $file..."

# Do some processing here

rm $file

done

Automated Remote File Transfer Script


Task: Automate the transfer of files from a local machine to a remote server.

#!/bin/bash

src_dir="/local/path"

dest_user="user"

dest_server="server.com"

dest_dir="/remote/path"

scp -r $src_dir $dest_user@$dest_server:$dest_dir

echo "Files transferred to $dest_server."

You might also like