0
0
Bash Scriptingscripting~30 mins

Service health check script in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Service health check script
📖 Scenario: You are a system administrator who wants to monitor if important services on your server are running. You will write a simple script that checks the status of these services and reports if they are active or not.
🎯 Goal: Build a bash script that checks the health of three services: nginx, mysql, and ssh. The script will print if each service is running or stopped.
📋 What You'll Learn
Create a list of service names in a variable
Create a variable to hold the command used to check service status
Use a loop to check each service status using the command
Print the service name and its status (running or stopped)
💡 Why This Matters
🌍 Real World
System administrators often need to monitor if critical services are running to keep servers healthy and avoid downtime.
💼 Career
Knowing how to write simple health check scripts is a basic skill for IT support, DevOps, and system administration roles.
Progress0 / 4 steps
1
Create a list of services to check
Create a variable called services that holds the names of these services exactly: nginx, mysql, and ssh separated by spaces.
Bash Scripting
Need a hint?

Use double quotes and separate service names with spaces inside the quotes.

2
Set the command to check service status
Create a variable called check_cmd and set it to the exact string systemctl is-active which will be used to check if a service is running.
Bash Scripting
Need a hint?

Assign the string exactly as shown to the variable check_cmd.

3
Check each service status using a loop
Use a for loop with variable service to iterate over $services. Inside the loop, run the command stored in $check_cmd with the current $service and store the output in a variable called status.
Bash Scripting
Need a hint?

Use command substitution with $( ) to run the command and save output.

4
Print the service name and its status
Inside the for loop, add a printf statement to print the service name and its status in this exact format: Service nginx is running or Service mysql is stopped. If status equals active, print 'running', otherwise print 'stopped'.
Bash Scripting
Need a hint?

Use an if statement to compare status with active. Use printf to format the output.