0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check if Service is Running

Use systemctl is-active --quiet service_name in a Bash script and check the exit status to see if the service is running; if the exit status is 0, the service is running.
📋

Examples

Inputservice_name=ssh
OutputService ssh is running
Inputservice_name=nonexistentservice
OutputService nonexistentservice is not running
Inputservice_name=cron
OutputService cron is running
🧠

How to Think About It

To check if a service is running, the script should ask the system for the service status using a command like systemctl is-active. Then, it should look at the command's exit code: if it is zero, the service is running; otherwise, it is not. This simple check avoids parsing output text and is reliable.
📐

Algorithm

1
Get the service name as input.
2
Run the command to check if the service is active.
3
Check the command's exit status.
4
If exit status is zero, print that the service is running.
5
Otherwise, print that the service is not running.
💻

Code

bash
#!/bin/bash
service_name="$1"

if systemctl is-active --quiet "$service_name"; then
  echo "Service $service_name is running"
else
  echo "Service $service_name is not running"
fi
Output
Service ssh is running
🔍

Dry Run

Let's trace checking the service 'ssh' through the code

1

Get service name

service_name="ssh"

2

Run systemctl check

systemctl is-active --quiet ssh (exit status 0 if running)

3

Check exit status

Exit status is 0, so service is running

4

Print result

Output: Service ssh is running

StepCommandExit StatusOutput
2systemctl is-active --quiet ssh0
4echo messageService ssh is running
💡

Why This Works

Step 1: Use systemctl is-active

The command systemctl is-active --quiet service_name checks if the service is running without printing output.

Step 2: Check exit status

If the exit status is 0, it means the service is active; any other status means it is not.

Step 3: Print user-friendly message

Based on the exit status, the script prints a clear message about the service status.

🔄

Alternative Approaches

Using service command
bash
#!/bin/bash
service_name="$1"

if service "$service_name" status > /dev/null 2>&1; then
  echo "Service $service_name is running"
else
  echo "Service $service_name is not running"
fi
Works on older systems without systemctl but output parsing may vary.
Using pidof command
bash
#!/bin/bash
service_name="$1"

if pidof "$service_name" > /dev/null; then
  echo "Service $service_name is running"
else
  echo "Service $service_name is not running"
fi
Checks if any process with the service name is running; may give false positives if multiple processes share the name.

Complexity: O(1) time, O(1) space

Time Complexity

The check runs a single system command which completes in constant time regardless of service state.

Space Complexity

The script uses a fixed amount of memory for variables and command execution, so space is constant.

Which Approach is Fastest?

Using systemctl is-active --quiet is fastest and most reliable on modern systems; alternatives may be slower or less accurate.

ApproachTimeSpaceBest For
systemctl is-activeO(1)O(1)Modern Linux systems with systemd
service statusO(1)O(1)Older Linux systems without systemd
pidofO(1)O(1)Quick process name check, less reliable
💡
Use systemctl is-active --quiet and check the exit code for a clean service status check.
⚠️
Parsing the output text of service status commands instead of checking the exit status can cause unreliable results.