Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'stop' instead of 'status' will change the service state instead of checking it.
✗ Incorrect
The 'systemctl status' command checks the current status of a service.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'enable' will not give the status output.
✗ Incorrect
The 'status' command outputs the current status, which we capture in a variable.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'running' or 'started' causes command errors.
✗ Incorrect
The correct command is 'systemctl is-active' to check if the service is running.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'is-running' instead of 'is-active'.
Using '!=' instead of '==' in the if condition.
✗ Incorrect
Use 'is-active' to check status and '==' to compare strings in the if condition.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'running' instead of 'active'.
Using '=' instead of '==' in the if condition.
Returning 1 for active status.
✗ Incorrect
The function uses 'is-active' to check status, '==' to compare, and returns 0 for active.