I want to run some script when a service fails. The closest thing I see to this is the FailureAction= option (under [Service] section), but it only offers reboot commands.

2 Answers 2


There is an OnFailure= directive in section [Unit], documented in systemd.unit(5). It is defined as follows:

A space-separated list of one or more units that are activated when this unit enters the "failed" state.

(Also there is an OnFailureJobMode= directive in the same section which allows to set job mode for activating OnFailure= units.)

  • Looks like those options start other units instead just scripts.
    – tshepang
    Apr 21, 2015 at 14:54
  • 3
    @Tshepang: Of course. In systemd, a unit is, well, a basic unit of doing anything. Write a simple unit for your script, put it under /etc/systemd/system and place its name in the OnFailure= directive.
    – intelfx
    Apr 21, 2015 at 14:57

You can also use ExecStopPost to run a command directly instead of starting a unit.

I wasn't happy with the OnFailure setting so I kept looking and found ExecStopPost.

The the following real example, if the main task fails then systemd will run a git command.

Description=SRI Dispenser Server



# This is task to run when this service starts
ExecStart=/usr/bin/python -m sri.DispenserServer

# If any of the ExecStart tasks fail, then ExecStopPost will run
ExecStopPost=/bin/git checkout -- .




ExecStopPost= Additional commands that are executed after the service is stopped. This includes cases where the commands configured in ExecStop= were used, where the service does not have any ExecStop= defined, or where the service exited unexpectedly. This argument takes multiple command lines, following the same scheme as described for ExecStart=. Use of these settings is optional. Specifier and environment variable substitution is supported. Note that – unlike ExecStop= – commands specified with this setting are invoked when a service failed to start up correctly and is shut down again.

It is recommended to use this setting for clean-up operations that shall be executed even when the service failed to start up correctly. Commands configured with this setting need to be able to operate even if the service failed starting up half-way and left incompletely initialized data around. As the service's processes have been terminated already when the commands specified with this setting are executed they should not attempt to communicate with them.

Note that all commands that are configured with this setting are invoked with the result code of the service, as well as the main process' exit code and status, set in the $SERVICE_RESULT, $EXIT_CODE and $EXIT_STATUS environment variables, see systemd.exec(5) for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .