0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Restart Service If Down

Use a Bash script with systemctl is-active --quiet servicename to check if the service is down, and if so, run systemctl restart servicename to restart it.
📋

Examples

Inputservice: sshd (running)
OutputService sshd is running. No action taken.
Inputservice: apache2 (inactive)
OutputService apache2 is down. Restarting... Service apache2 restarted successfully.
Inputservice: nonexistentservice
OutputService nonexistentservice is down. Restarting... Failed to restart nonexistentservice.
🧠

How to Think About It

First, check the service status quietly using systemctl is-active --quiet. If the service is not active, then restart it using systemctl restart. Finally, print messages to inform if the service was running or restarted.
📐

Algorithm

1
Check if the service is active using a system command.
2
If the service is active, print that it is running.
3
If the service is not active, restart the service.
4
Print a message indicating the service was restarted or if the restart failed.
💻

Code

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

if systemctl is-active --quiet "$SERVICE"; then
  echo "Service $SERVICE is running. No action taken."
else
  echo "Service $SERVICE is down. Restarting..."
  if systemctl restart "$SERVICE"; then
    echo "Service $SERVICE restarted successfully."
  else
    echo "Failed to restart $SERVICE."
  fi
fi
Output
Service apache2 is down. Restarting... Service apache2 restarted successfully.
🔍

Dry Run

Let's trace the script with input 'apache2' when the service is down.

1

Check service status

Command: systemctl is-active --quiet apache2 returns false (service down)

2

Print service down message

Output: Service apache2 is down. Restarting...

3

Restart service

Command: systemctl restart apache2 returns success

4

Print restart success message

Output: Service apache2 restarted successfully.

StepActionResult
1Check if apache2 is activeNo (service down)
2Print down messageService apache2 is down. Restarting...
3Restart apache2Success
4Print success messageService apache2 restarted successfully.
💡

Why This Works

Step 1: Check service status quietly

The script uses systemctl is-active --quiet to check if the service is running without printing output.

Step 2: Decide action based on status

If the service is active, it prints a message and does nothing; if not, it proceeds to restart.

Step 3: Restart the service

The script runs systemctl restart to start the service again if it was down.

Step 4: Print result message

It prints whether the restart succeeded or failed to inform the user.

🔄

Alternative Approaches

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

if service "$SERVICE" status > /dev/null 2>&1; then
  echo "Service $SERVICE is running. No action taken."
else
  echo "Service $SERVICE is down. Restarting..."
  if service "$SERVICE" restart; then
    echo "Service $SERVICE restarted successfully."
  else
    echo "Failed to restart $SERVICE."
  fi
fi
Uses the older <code>service</code> command, which works on some systems but is less modern than <code>systemctl</code>.
Using pidof to check process
bash
#!/bin/bash
SERVICE="$1"

if pidof "$SERVICE" > /dev/null; then
  echo "Service $SERVICE is running. No action taken."
else
  echo "Service $SERVICE is down. Restarting..."
  if systemctl restart "$SERVICE"; then
    echo "Service $SERVICE restarted successfully."
  else
    echo "Failed to restart $SERVICE."
  fi
fi
Checks if the service process is running by name, which may not work for all services but avoids systemctl dependency.

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

Time Complexity

The script runs a fixed number of system commands regardless of input size, so it is constant time O(1).

Space Complexity

The script uses a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

Using systemctl is-active --quiet is efficient and standard on modern Linux systems; alternatives like pidof may be faster but less reliable.

ApproachTimeSpaceBest For
systemctl is-activeO(1)O(1)Modern Linux systems with systemd
service commandO(1)O(1)Older Linux systems without systemd
pidof process checkO(1)O(1)Simple process presence check, less reliable
💡
Always run your script with proper permissions to restart services, usually as root or with sudo.
⚠️
Beginners often forget to check the service status quietly and get unwanted output interfering with script logic.