I have a service running software that generates some configuration files if they don't exist, and read them if they do exist. The problem I have been facing is that these files sometimes get corrupt, making the software unable to start, and thus making the service fail. In this case I would like to remove these files and restart the service.
I tried creating a service that should get executed in case of failure, by doing this:
[Service] ExecStart=/bin/run_program OnFailure=software-fail.service
where this service is:
[Service] ExecStart=/bin/rm /file/to/delete ExecStop=systemctl --user start software.service
The problem, however, is that this service doesn't start, even when the service has failed.
I tried doing
systemctl --user enable software-fail.service
but then it starts every time the system starts, just like any other service.
My temporary solution is to use
but this is not a satisfying way of solving it, as it will always delete the file upon stopping the service, no matter if it was because of failure or not.
Output when failing:
● software.service - Software Loaded: loaded (/home/trippelganger/.config/systemd/user/software.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2018-05-04 09:05:26 CEST; 5s ago Process: 1839 ExecStart=/bin/run_program (code=exited, status=1/FAILURE) Main PID: 1839 (code=exited, status=1/FAILURE) May 04 09:05:26 trippelganger systemd: software.service: Main process exited, code=exited, status=1/FAILURE May 04 09:05:26 trippelganger systemd: software.service: Unit entered failed state. May 04 09:05:26 trippelganger systemd: software.service: Failed with result 'exit-code'.
Output of systemctl --user status software-fail.service is:
● software-fail.service - Delete corrupt files Loaded: loaded (/home/trippelganger/.config/systemd/user/software-fail.service; disabled; vendor preset: enabled) Active: inactive (dead)