2

I've gone over the documentation for systemd here https://www.freedesktop.org/software/systemd/man/systemd.service.html.

It states in the ExecStartPre=,ExecStartPost= section

Note that if any of the commands specified in ExecStartPre=, ExecStart=, or ExecStartPost= fail (and are not prefixed with "-", see above) or time out before the service is fully up, execution continues with commands specified in ExecStopPost=, the commands in ExecStop= are skipped

I have an ExecStartPre rule being

ExecStartPre=/bin/false

In ExecStopPost I have an /bin/echo "I'm at ExecStopPost".

The service looks like

[Unit]
Description=Test

[Service]
Type=oneshot
ExecStartPre=/bin/echo "I'm at ExecStartPre"
ExecStartPre=/bin/false
ExecStart=/bin/echo "Running Test"
ExecStopPost=/bin/echo "I'm at ExecStopPost"

When it runs and fails, I never see it. Output from syslog looks like

Jun 15 20:48:01 ip-10-0-0-246 echo[8687]: I'm at ExecStartPre

Jun 15 20:48:01 ip-10-0-0-246 systemd[1]: test.service: Control process exited, code=exited status=1

Jun 15 20:48:01 ip-10-0-0-246 systemd[1]: Failed to start test.

Jun 15 20:48:01 ip-10-0-0-246 systemd[1]: test.service.service: Unit entered failed state.

Jun 15 20:48:01 ip-10-0-0-246 systemd[1]: test.service.service: Failed with result 'exit-code'.

I never see my echo.

journalctl -u test.service of course also has this as it's entry.

Do I need to configure this slightly differently? Or do I need to use OnFailure?

I need to be able to restart a Conflicts= on success or failure cases. This will be used in oneshot services triggered by timers.

Oh and this is running on 16.04.6 LTS.

0

It appears that oneshot services don't call ExecStopPost on failure. This link discusses it https://bugzilla.redhat.com/show_bug.cgi?id=1353039.

The relevant post being

ExecStopPost is executed only when unit transitions from running state (even when unit is killed) or when Main PID exits successfully while in activating state and service is not of forking type.

Difference is that in case of oneshot service while configured ExecStart actions are executing, service is still in activating state and not running. However in case of simple service, systemd immediately transitions service to running state, systemctl doesn't block and exit 0 is returned even when ExecStart fails (i.e. ExecStart=/bin/false). Thus oneshot service must exit cleanly in order for ExecStopPost to be called. AFAICT, option RemainAfterExit doesn't have an impact on ExecStopPost.

The solution ended up being adding an OnFailure to invoke the necessary items which would have been called in ExecStopPost

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.