0
0
Bash Scriptingscripting~10 mins

Service health check script in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the service is running using systemctl.

Bash Scripting
if systemctl [1] nginx.service; then
  echo "Service is running"
else
  echo "Service is not running"
fi
Drag options to blanks, or click blank then click option'
Astatus
Bstart
Cstop
Drestart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'stop' instead of 'status' will change the service state instead of checking it.
2fill in blank
medium

Complete the code to assign the service status output to a variable.

Bash Scripting
service_status=$(systemctl [1] nginx.service)
Drag options to blanks, or click blank then click option'
Astart
Bdisable
Cenable
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'enable' will not give the status output.
3fill in blank
hard

Fix the error in the if condition to check if the service is active.

Bash Scripting
if systemctl is-[1] nginx.service; then
  echo "Service is active"
else
  echo "Service is inactive"
fi
Drag options to blanks, or click blank then click option'
Aactive
Brunning
Cenabled
Dstarted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'running' or 'started' causes command errors.
4fill in blank
hard

Fill both blanks to create a loop that checks the service status every 5 seconds and breaks if active.

Bash Scripting
while true; do
  status=$(systemctl is-[1] nginx.service)
  if [ "$status" [2] "active" ]; then
    echo "Service is active"
    break
  fi
  sleep 5
done
Drag options to blanks, or click blank then click option'
Aactive
B==
C!=
Drunning
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'is-running' instead of 'is-active'.
Using '!=' instead of '==' in the if condition.
5fill in blank
hard

Fill all three blanks to create a function that checks service status and returns 0 if active, 1 otherwise.

Bash Scripting
check_service() {
  local status=$(systemctl is-[1] $1)
  if [ "$status" [2] "active" ]; then
    return [3]
  else
    return 1
  fi
}
Drag options to blanks, or click blank then click option'
Aactive
B==
C0
Drunning
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'running' instead of 'active'.
Using '=' instead of '==' in the if condition.
Returning 1 for active status.