1

On Ubuntu 22.04 server, I have a bash script that reloads nagios4 config. After the reload I would like to check if the service is fine

#!/bin/bash sudo systemctl reload nagios4.service if (systemctl --quiet is-failed nagios4.service); then echo "Bad config!" fi 

somehow it's not working, if the config is not ok, the service fails but this is not recognized by systemctl is-failed

if manually I check systemctl status nagios4

× nagios4.service - nagios4 Loaded: loaded (/lib/systemd/system/nagios4.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2024-06-05 09:20:03 CEST; 4min 27s ago 

I have tried to change my script to use restart instead of reload or to run systemctl is-failed with sudo, but nothing changes.

What am I missing?


Update: reload return status is 0 even on failure

sudo systemctl reload nagios4.service status=$? echo $status 
2
  • I wonder what's the return code of the failed reload command is. Commented Jun 5, 2024 at 11:00
  • @TomYan reload has return code 0 Commented Jun 5, 2024 at 12:57

2 Answers 2

1

The reload command sends a signal to the service to reload its configuration without restarting the entire service. If the reload fails due to a bad configuration, the service might not immediately transition to a "failed" state that systemctl is-failed can detect.

One reliable way is to check the status of the service directly after the reload, rather than relying on the exit status of the reload command itself. Here’s how you can adjust your script:

#!/bin/bash # Reload the service sudo systemctl reload nagios4.service # Wait a moment to ensure the service has time to fail if the config is bad sleep 2 # Check the service status if systemctl is-active --quiet nagios4.service; then echo "Reload successful!" else echo "Bad config!" fi 
1
  • Thanks, so systemctl reload is kind of async. I am pretty sure that waiting 2 seconds would be the solution in 99% of times. But in my experience waiting for an arbitrary time is risky. What if the reload takes longer? Commented Jun 6, 2024 at 4:48
0

Don’t use external scripts to check systemd state to begin with, use systemd to run scripts on certain states.

Lots of useful examples on how you can use OnFailure etc here: https://unix.stackexchange.com/questions/441575/proper-way-to-use-onfailure-in-systemd

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.