We sometimes disable specific servers in HAProxy (running on RedHat 7, but compiled locally because 1.8 is not available from RedHat) using an admin socket and we want the state to be maintained if the server is restarted. We set HAProxy to load its state from a file with server-state-file, but we need to remember to save the state file every time we make a change. Is there a good way to make sure the state is saved every time the server stops?
If systemd had ExecPreStop=, we could include it from a file in /etc/systemd/system without modifying the distributed unit file, but systemd developers said they don't see the need for that directive.


We ended up creating a "fake" service with just a stop action that depends on haproxy.service and therefor will have its ExecStop run before HAProxy is stopped.

haproxy-state.service :

Description=Save HAProxy State Before Service Stop

ExecStop=/bin/sh -c "/usr/bin/socat /run/haproxy/control.socket - <<< \"show servers state\" > /var/lib/haproxy/last_state"

WantedBy=multi-user.target haproxy.service

It needs to be after haproxy.service so it will stop before. It needs to be WantedBy so that it will always be started any time HAProxy is running.

This isn't perfect because the state could change if there is a long pause before systemd stops the next service, but that is a risk we will take.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.