0
0
Bash Scriptingscripting~20 mins

Service health check script in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Health Check Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this service health check snippet?
Consider this bash script snippet that checks if the nginx service is active and prints a message accordingly. What will it output if nginx is running?
Bash Scripting
if systemctl is-active --quiet nginx; then
  echo "nginx is running"
else
  echo "nginx is not running"
fi
Anginx is running
Bsyntax error
Ccommand not found error
Dnginx is not running
Attempts:
2 left
💡 Hint
The command 'systemctl is-active --quiet' returns success if the service is active.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this health check script
This script aims to check if the apache2 service is running and print a message. Which option correctly identifies the syntax error?
Bash Scripting
if systemctl is-active apache2; then
  echo "apache2 is running"
else
  echo "apache2 is not running"
fi
AMissing '--quiet' option causes output to appear but no syntax error
BMissing semicolon after 'then' causes syntax error
CIncorrect use of brackets causes syntax error
DMissing 'then' keyword causes syntax error
Attempts:
2 left
💡 Hint
Check if the script syntax is valid but consider the command behavior.
🔧 Debug
advanced
2:00remaining
Why does this health check script always print 'Service is not running'?
This script checks if the mysql service is running but always prints 'Service is not running' even when mysql is active. What is the cause?
Bash Scripting
if systemctl is-active mysql; then
  echo "Service is running"
else
  echo "Service is not running"
fi
AThe command 'systemctl is-active mysql' returns output but exit code is non-zero, causing else branch
BThe script lacks '--quiet' so output interferes with condition evaluation
CThe 'if' condition checks output string instead of exit code, causing wrong branch
DThe script uses wrong service name causing failure
Attempts:
2 left
💡 Hint
Check how bash evaluates the 'if' condition with commands that output text.
🚀 Application
advanced
2:30remaining
Which script correctly checks multiple services and reports their status?
You want a bash script to check if 'nginx' and 'redis' services are running and print their status lines. Which script does this correctly?
A
for svc in nginx redis
  if systemctl is-active --quiet $svc; then
    echo "$svc is running"
  else
    echo "$svc is not running"
  fi
done
B
for svc in nginx redis; do
  if systemctl status $svc; then
    echo "$svc is running"
  else
    echo "$svc is not running"
  fi
done
C
for svc in nginx redis; do
  if systemctl is-active $svc; then
    echo "$svc is running"
  else
    echo "$svc is not running"
  fi
done
D
for svc in nginx redis; do
  if systemctl is-active --quiet $svc; then
    echo "$svc is running"
  else
    echo "$svc is not running"
  fi
done
Attempts:
2 left
💡 Hint
Check for correct loop syntax and quiet status check.
🧠 Conceptual
expert
3:00remaining
What is the best way to handle transient service states in a health check script?
When a service may be restarting or temporarily inactive, how should a bash health check script handle this to avoid false 'not running' reports?
AUse 'systemctl status' once and parse output for 'running' keyword
BRun 'systemctl is-active --quiet' multiple times with delays and confirm consistent inactive status before reporting
CCheck service logs instead of status commands for more accurate state
DAssume service is running if last check was successful, ignore current status
Attempts:
2 left
💡 Hint
Transient states can cause momentary false negatives; repeated checks help confirm.