Mastering Shell Scripting: 15 Essential Scenario-Based Interview Questions for DevOps
TLDR: This blog post covers 15 scenario-based shell scripting interview questions commonly encountered in DevOps interviews, providing detailed answers and scripts for automating tasks such as application deployment, disk usage monitoring, service management, and more.
In today's fast-paced tech environment, shell scripting is a crucial skill for DevOps professionals. This blog post will explore 15 scenario-based interview questions that you might encounter during a DevOps interview, along with detailed answers and scripts to help you automate various tasks. Whether you're preparing for an interview or looking to sharpen your skills, this guide is for you.
1. Automating Application Deployment to Multiple Servers
Question: How would you automate the deployment of an application to multiple servers using a shell script?
To automate the deployment, you can write a script that copies the application to multiple servers and restarts the service on each server. Here’s a basic outline of the script:
servers=("server1" "server2" "server3")
source_path="/path/to/application"
destination_path="/path/to/destination"
for server in "${servers[@]}"; do
scp $source_path $server:$destination_path
ssh $server "systemctl restart apache2"
done
This script uses scp
for secure copying and ssh
to execute commands on remote machines.
2. Monitoring Disk Usage
Question: Create a script to monitor disk usage and send an alert if usage exceeds 80%.
Monitoring disk usage is essential to prevent system issues. Here’s how you can do it:
threshold=80
usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ $usage -gt $threshold ]; then
echo "Disk usage is above $threshold%"
# Integrate email alert mechanism here
fi
This script checks the disk usage and alerts if it exceeds the specified threshold.
3. Checking and Starting Services
Question: Write a script to check if a service is running and start it if it's not.
Ensuring critical services are running is vital. Here’s a script to check and start a service:
service_name="httpd"
status=$(systemctl is-active $service_name)
if [ "$status" != "active" ]; then
echo "$service_name is not running. Starting it now."
systemctl start $service_name
else
echo "$service_name is already running."
fi
4. Backing Up Logs Older Than 7 Days
Question: Write a script to backup logs older than 7 days and delete the original files.
Regular backups are crucial for data management. Here’s a script to archive old logs:
log_directory="/path/to/logs"
backup_directory="/path/to/backup"
find $log_directory -type f -mtime +7 -exec gzip {} \; -exec mv {} $backup_directory \;
This script finds logs older than 7 days, compresses them, and moves them to a backup directory.
5. Automating Database Backups
Question: Create a script to automate database backup.
Automating database backups ensures data safety. Here’s a sample script for MySQL:
DB_NAME="your_database"
USER="your_username"
PASSWORD="your_password"
BACKUP_DIR="/path/to/backup"
mysqldump -u $USER -p$PASSWORD $DB_NAME > $BACKUP_DIR/$DB_NAME-$(date +%F).sql
echo "Backup completed."
6. Rotating Logs Weekly
Question: Write a script to rotate logs on a weekly basis.
Log rotation helps manage disk space. Here’s a script to move logs to an archive directory:
log_directory="/path/to/logs"
archive_directory="/path/to/archive"
find $log_directory -type f -mtime +7 -exec mv {} $archive_directory \;
7. Checking Status of Multiple Services
Question: Write a script to check the status of multiple services and restart any that are not running.
You can check multiple services using an array:
services=("httpd" "nginx" "mysql")
for service in "${services[@]}"; do
status=$(systemctl is-active $service)
if [ "$status" != "active" ]; then
echo "$service is not running. Restarting it."
systemctl restart $service
else
echo "$service is running."
fi
done
8. Updating a Web Application from Git
Question: Create a script to update a web application by pulling the latest code from a Git repository.
Automating web application updates can be done as follows:
web_directory="/path/to/webapp"
git_repo="https://github.com/user/repo.git"
cd $web_directory
git pull $git_repo
systemctl restart apache2
9. Compressing Old Log Files
Question: Write a script to compress and archive old log files.
To compress logs older than 30 days:
log_directory="/path/to/logs"
archive_directory="/path/to/archive"
find $log_directory -type f -mtime +30 -exec gzip {} \; -exec mv {} $archive_directory \;
10. Cleaning Up Temporary Files
Question: Write a script to automate the cleanup of temporary files older than 10 days.
Cleaning up temporary files can be automated as follows:
temp_directory="/path/to/temp"
find $temp_directory -type f -mtime +10 -exec rm {} \;
echo "Temporary files older than 10 days have been deleted."
11. Monitoring CPU Usage
Question: Write a script to monitor CPU usage and alert if it exceeds a certain threshold.
Monitoring CPU usage is crucial for performance:
threshold=80
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
if (( $(echo "$cpu_usage > $threshold" | bc -l) )); then
echo "CPU usage is above $threshold%"
# Integrate email alert mechanism here
fi
12. Installing a List of Packages
Question: Write a script to install a list of packages.
Automating package installation can be done as follows:
packages=("git" "curl" "vim")
for package in "${packages[@]}"; do
if ! dpkg -l | grep -q $package; then
apt-get install -y $package
else
echo "$package is already installed."
fi
done
13. Synchronizing Local and Remote Directories
Question: Write a script to sync a local directory with a remote directory using the rsync command.
To synchronize directories:
local_directory="/path/to/local"
remote_user="user"
remote_host="remote_host"
remote_directory="/path/to/remote"
rsync -avz $local_directory $remote_user@$remote_host:$remote_directory
14. Checking Health of a Web Application
Question: Write a script to check the health of a web application by sending an HTTP request.
To check the health of a web application:
url="http://yourwebapp.com"
expected_code=200
response_code=$(curl -o /dev/null -s -w "%{http_code}" $url)
if [ $response_code -ne $expected_code ]; then
echo "Web application is not healthy."
else
echo "Web application is healthy."
fi
15. Automating Server Configuration
Question: Write a script to automate the configuration of a new server with necessary packages and settings.
To automate server configuration:
packages=("nginx" "git")
firewall_rules=("allow 80" "allow 443")
apt-get update
for package in "${packages[@]}"; do
apt-get install -y $package
done
for rule in "${firewall_rules[@]}"; do
ufw $rule
done
Conclusion
These 15 scenario-based shell scripting questions cover essential tasks that DevOps professionals may encounter. Mastering these scripts will not only prepare you for interviews but also enhance your automation skills in real-world applications. If you found this guide helpful, consider subscribing for more content and tips on DevOps and shell scripting.