Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a service health check script in bash?
easy
A. To install new software packages
B. To check if a service is running or stopped
C. To delete files from the system
D. To create new user accounts

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To check if a service is running or stopped -> Option B
  4. Quick Check:

    Service health check = check running status [OK]
Hint: Focus on what 'health check' means for a service [OK]
Common Mistakes:
  • Confusing service check with installation
  • Thinking it manages users
  • Assuming it deletes files
2. Which of the following is the correct syntax to check the status of a service named nginx using systemctl in bash?
easy
A. systemctl status nginx
B. systemctl check nginx
C. service nginx status
D. systemctl run nginx

Solution

  1. Step 1: Recall systemctl command syntax

    The correct command to check status is systemctl status servicename.
  2. 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.
  3. Final Answer:

    systemctl status nginx -> Option A
  4. Quick Check:

    systemctl status = check service [OK]
Hint: Remember: systemctl status + service name checks status [OK]
Common Mistakes:
  • Using 'check' instead of 'status'
  • Confusing 'service' and 'systemctl'
  • Using 'run' instead of 'status'
3. What will be the output of this script snippet if the ssh service is running?
if systemctl is-active --quiet ssh; then
  echo "SSH service is running"
else
  echo "SSH service is stopped"
fi
medium
A. No output
B. SSH service is stopped
C. Syntax error
D. SSH service is running

Solution

  1. Step 1: Understand systemctl is-active --quiet

    This command returns success (exit code 0) if the service is active (running), otherwise failure.
  2. Step 2: Follow the if-else logic

    If the service is running, the then branch echoes "SSH service is running"; else branch echoes stopped.
  3. Final Answer:

    SSH service is running -> Option D
  4. Quick Check:

    is-active true = running message [OK]
Hint: is-active --quiet returns success if running [OK]
Common Mistakes:
  • Assuming output is from systemctl status
  • Confusing exit codes with output text
  • Missing the echo command effect
4. Identify the error in this health check script snippet:
if systemctl status apache2; then
  echo "Apache2 is running"
else
  echo "Apache2 is stopped"
fi
medium
A. Missing --quiet option causes wrong output
B. The if statement syntax is incorrect
C. systemctl status returns output but not exit code for running check
D. echo commands are missing quotes

Solution

  1. 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.
  2. Step 2: Understand correct command for status check

    Use systemctl is-active --quiet to get proper exit code check without any output.
  3. Final Answer:

    systemctl status returns output but not exit code for running check -> Option C
  4. Quick Check:

    Use is-active for exit code check [OK]
Hint: Use is-active --quiet for exit code, not status [OK]
Common Mistakes:
  • Assuming systemctl status exit code indicates running
  • Syntax errors in if statement
  • Forgetting quotes in echo
5. You want to write a bash script that checks multiple services: nginx, mysql, and redis. Which script snippet correctly loops over these services and prints their status using systemctl is-active --quiet?
hard
A. 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
B. for service in [nginx, mysql, redis]; do if systemctl status $service; then echo "$service is running" else echo "$service is stopped" fi done
C. for service in nginx mysql redis do systemctl is-active $service echo "$service status checked" done
D. 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

Solution

  1. 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.
  2. 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 uses systemctl is-active --quiet correctly to check service status and prints appropriate message.
  3. 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 A
  4. Quick 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]
Hint: Use for service in list; do ... done with is-active --quiet [OK]
Common Mistakes:
  • Using brackets [] for list in for loop
  • Adding $ in for variable declaration
  • Not using is-active --quiet for status check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard