0
0
Bash Scriptingscripting~10 mins

Service health check script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Service health check script
Start Script
Check Service Status
Is Service Active?
NoPrint Service Down
Exit with Error
Print Service Running
Exit with Success
The script starts, checks if the service is active, prints status, and exits accordingly.
Execution Sample
Bash Scripting
service_name="nginx"
if systemctl is-active --quiet "$service_name"; then
  echo "$service_name is running"
  exit 0
else
  echo "$service_name is NOT running"
  exit 1
fi
This script checks if the 'nginx' service is running and prints the result.
Execution Table
StepActionCommand/CheckResultOutput
1Set service_name variableservice_name="nginx"service_name=nginx
2Check if service is activesystemctl is-active --quiet "$service_name"Exit code 0 (active)
3Condition checkif exit code == 0True
4Print running messageecho "$service_name is running"Printednginx is running
5End scriptexit 0Success
💡 Script ends after printing service status based on active check.
Variable Tracker
VariableStartAfter Step 1Final
service_nameundefinednginxnginx
Key Moments - 3 Insights
Why do we use 'systemctl is-active --quiet' instead of just 'systemctl status'?
Using 'systemctl is-active --quiet' returns only an exit code without extra text, making it easy to check service status in conditions (see execution_table step 2 and 3).
What does the exit code 0 mean in the context of 'systemctl is-active --quiet'?
Exit code 0 means the service is active/running, which triggers the 'if' branch to print the running message (see execution_table step 3).
What happens if the service is not running?
The else branch runs, printing the service is NOT running message and the script exits with a non-zero exit code (not shown in this run but implied by the else branch).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'service_name' after step 1?
A"apache"
Bundefined
C"nginx"
D"systemctl"
💡 Hint
Check variable_tracker row for 'service_name' after step 1.
At which step does the script decide the service is running?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the condition check in execution_table step 3.
If the service was not running, which step would print the NOT running message?
AA new else step after step 3
BStep 4
CStep 3
DStep 5
💡 Hint
Consider the else branch after the condition check in the script.
Concept Snapshot
Service health check script in bash:
- Use 'systemctl is-active --quiet service' to check status
- Check exit code: 0 means running
- Use if-else to print status message
- Exit 0 if running, non-zero if down
- Simple, clear, and effective for monitoring
Full Transcript
This script sets a variable for the service name, then uses 'systemctl is-active --quiet' to check if the service is running. It checks the exit code: if zero, it prints the service is running; otherwise, it prints the service is not running. The script then exits. This method is clean and efficient for service health checks.