Restarting HAProxy systemd service from bash gives wrong return code

I have a few HAProxy installs and some scripts to automate stuff.

When I restart HAProxy manually, everything is ok:

root@hapxtest:~# systemctl restart haproxy
root@hapxtest:~# echo $?
0

However, when I execute the same command from either bash, I get return code 2:

haproxy -c -V -f $HAPROXYCONFIG

if [ $? -eq 0 ]; then
    echo Reloading HAProxy

    systemctl restart haproxy

# TODO: For some reason we always get rc 2
#        if [ $? -eq 0 ]; then
#            echo SOS: Something went wrong while reloading HAProxy
#            exit 2
#        fi

I really cannot figure out why. Any idea?

You are setting the return code to 2 here (exit 2), after you test that the return code of of the systemctl command is 0 ($? -eq 0).

I think that second if condition has to be “not equal” $? -ne 0.

Well, that is stupid. I’ve been staring at it for too long :slight_smile:

1 Like