Bash Script to Restart Service If Down
systemctl is-active --quiet servicename to check if the service is down, and if so, run systemctl restart servicename to restart it.Examples
How to Think About It
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
Code
#!/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
Dry Run
Let's trace the script with input 'apache2' when the service is down.
Check service status
Command: systemctl is-active --quiet apache2 returns false (service down)
Print service down message
Output: Service apache2 is down. Restarting...
Restart service
Command: systemctl restart apache2 returns success
Print restart success message
Output: Service apache2 restarted successfully.
| Step | Action | Result |
|---|---|---|
| 1 | Check if apache2 is active | No (service down) |
| 2 | Print down message | Service apache2 is down. Restarting... |
| 3 | Restart apache2 | Success |
| 4 | Print success message | Service 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
#!/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
#!/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
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| systemctl is-active | O(1) | O(1) | Modern Linux systems with systemd |
| service command | O(1) | O(1) | Older Linux systems without systemd |
| pidof process check | O(1) | O(1) | Simple process presence check, less reliable |