Service health check script in Bash Scripting - Time & Space Complexity
When running a service health check script, it's important to know how the time it takes grows as you check more services.
We want to understand how the script's work increases when the number of services grows.
Analyze the time complexity of the following code snippet.
services=("nginx" "mysql" "redis" "docker")
for service in "${services[@]}"; do
systemctl is-active --quiet "$service"
if [ $? -eq 0 ]; then
echo "$service is running"
else
echo "$service is NOT running"
fi
done
This script checks if each service in the list is running and prints its status.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each service to check its status.
- How many times: Once for each service in the list.
As the number of services increases, the script runs the check for each one separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of services.
Time Complexity: O(n)
This means the time to run the script grows in a straight line as you add more services to check.
[X] Wrong: "The script runs in the same time no matter how many services are checked."
[OK] Correct: Each service requires a separate check, so more services mean more work and more time.
Understanding how loops affect script time helps you write efficient checks and explain your reasoning clearly in interviews.
"What if we added nested loops to check multiple properties per service? How would the time complexity change?"
Practice
service health check script in bash?Solution
Step 1: Understand the script's goal
A service health check script is designed to verify the status of a service, whether it is running or stopped.Step 2: Compare options with the goal
Only To check if a service is running or stopped matches the purpose of checking service status; others are unrelated tasks.Final Answer:
To check if a service is running or stopped -> Option BQuick Check:
Service health check = check running status [OK]
- Confusing service check with installation
- Thinking it manages users
- Assuming it deletes files
nginx using systemctl in bash?Solution
Step 1: Recall systemctl command syntax
The correct command to check status issystemctl status servicename.Step 2: Match options with correct syntax
systemctl status nginx matches the correct syntax. systemctl check nginx uses a wrong verb 'check'. service nginx status uses 'service' command which is older but not systemctl. systemctl run nginx uses 'run' which is invalid here.Final Answer:
systemctl status nginx -> Option AQuick Check:
systemctl status = check service [OK]
- Using 'check' instead of 'status'
- Confusing 'service' and 'systemctl'
- Using 'run' instead of 'status'
ssh service is running?if systemctl is-active --quiet ssh; then echo "SSH service is running" else echo "SSH service is stopped" fi
Solution
Step 1: Understand systemctl is-active --quiet
This command returns success (exit code 0) if the service is active (running), otherwise failure.Step 2: Follow the if-else logic
If the service is running, the then branch echoes "SSH service is running"; else branch echoes stopped.Final Answer:
SSH service is running -> Option DQuick Check:
is-active true = running message [OK]
- Assuming output is from systemctl status
- Confusing exit codes with output text
- Missing the echo command effect
if systemctl status apache2; then echo "Apache2 is running" else echo "Apache2 is stopped" fi
Solution
Step 1: Check systemctl status behavior
systemctl status outputs verbose service information to stdout every time it is called, which pollutes the output of the health check script with extra details before the echo. Although it returns exit code 0 if active and non-zero (e.g., 3) if stopped, the verbose output makes it unsuitable for clean scripting.Step 2: Understand correct command for status check
Usesystemctl is-active --quietto get proper exit code check without any output.Final Answer:
systemctl status returns output but not exit code for running check -> Option CQuick Check:
Use is-active for exit code check [OK]
- Assuming systemctl status exit code indicates running
- Syntax errors in if statement
- Forgetting quotes in echo
nginx, mysql, and redis. Which script snippet correctly loops over these services and prints their status using systemctl is-active --quiet?Solution
Step 1: Check loop syntax and service list
for service in nginx mysql redis; do if systemctl is-active --quiet $service; then echo "$service is running" else echo "$service is stopped" fi done uses correct bash for loop syntax with space-separated service names. for service in [nginx, mysql, redis]; do if systemctl status $service; then echo "$service is running" else echo "$service is stopped" fi done uses invalid list syntax with brackets. for service in nginx mysql redis do systemctl is-active $service echo "$service status checked" done misses if condition and uses wrong output. services=(nginx mysql redis) for service in ${services[@]}; do if systemctl is-active --quiet $service; then echo "$service is running" else echo "$service is stopped" fi done uses correct array and loop syntax without $ in variable declaration.Step 2: Verify status check command usage
for service in nginx mysql redis; do if systemctl is-active --quiet $service; then echo "$service is running" else echo "$service is stopped" fi done usessystemctl is-active --quietcorrectly to check service status and prints appropriate message.Final Answer:
for service in nginx mysql redis; do if systemctl is-active --quiet $service; then echo "$service is running" else echo "$service is stopped" fi done -> Option AQuick Check:
Correct loop + is-active --quiet = for service in nginx mysql redis; do if systemctl is-active --quiet $service; then echo "$service is running" else echo "$service is stopped" fi done [OK]
- Using brackets [] for list in for loop
- Adding $ in for variable declaration
- Not using is-active --quiet for status check
Ready to prove it?
Pass the test and this pattern moves unknown
