DevOps Interview sample Bash script with Explanations
These examples provide simple solutions to common DevOps interview questions, along with detailed explanations of each component.
DevOps Interview sample Bash script with Explanations
1. Bash Script to Check and Start a Service
This script monitors a service, starts it if not running, and logs all actions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
# Service name (can be changed to any service)
SERVICE_NAME="nginx"
LOG_FILE="service.log"
# Check if service is running
if systemctl is-active --quiet $SERVICE_NAME; then
echo "$(date): $SERVICE_NAME is running." | tee -a $LOG_FILE
else
echo "$(date): $SERVICE_NAME is not running. Starting service..." | tee -a $LOG_FILE
# Try to start the service and log the output
systemctl start $SERVICE_NAME 2>&1 | tee -a $LOG_FILE
# Check if service started successfully
if systemctl is-active --quiet $SERVICE_NAME; then
echo "$(date): $SERVICE_NAME started successfully." | tee -a $LOG_FILE
else
echo "$(date): Failed to start $SERVICE_NAME." | tee -a $LOG_FILE
fi
fi
Line-by-Line Explanation
#!/bin/bash- This is called a “shebang” line
- It tells the system to use the Bash shell to interpret this script
- Essential for any bash script to run properly
SERVICE_NAME="nginx"andLOG_FILE="service.log"- These are variables that store the name of the service to check and the log file path
- Using variables makes the script reusable and easy to modify
- You can change “nginx” to any service name (like “apache2”, “mysql”, etc.)
if systemctl is-active --quiet $SERVICE_NAME; then- This checks if the specified service is currently running
systemctlis the command used to control services in modern Linux systemsis-activechecks the service status--quietsuppresses normal output, giving only the exit code- The script uses this exit code to determine if the service is running
echo "$(date): $SERVICE_NAME is running." | tee -a $LOG_FILE- Logs that the service is running
$(date)inserts the current timestamptee -a $LOG_FILEsends the output to both the terminal and appends it to the log file-ameans “append” instead of overwriting the log file
elsestatement- The code in this block runs if the service is NOT running
systemctl start $SERVICE_NAME 2>&1 | tee -a $LOG_FILE- Attempts to start the service
2>&1redirects both standard output and errors to the pipe- This ensures that error messages are also captured in the log file
- Second
ifcheck- After trying to start the service, this verifies if the start attempt was successful
- This double-check is important for proper error handling
- Final status messages
- The script logs whether the service was successfully started or if it failed
- This creates a clear audit trail of actions and results
Why This Script Works Well for Interviews
- It demonstrates knowledge of service management with
systemctl - It shows proper logging practices with timestamps
- It implements error handling and status checking
- It uses variables for flexibility and maintainability
- It’s simple enough to explain quickly but complete enough to be functional ~eof~
This post is licensed under CC BY 4.0 by the author.