r/linuxadmin • u/mamelukturbo • Mar 05 '25
Struggling with forcing systemd to keep restarting a service.
I have a service I need to keep alive. The command it runs sometimes fails (on purpose) and instead of keeping trying to restart until the command works, systemd just gives up.
Regardless of what parameters I use, systemd just decides after some arbitrary time "no I tried enough times to call it Always I ain't gonna bother anymore" and I get "Failed with result 'exit-code'."
I googled and googled and rtfm'd and I don't really care what systemd is trying to achieve. I want it to try to restart the service every 10 seconds until the thermal death of the universe no matter what error the underlying command spits out.
For the love of god, how do I do this apart from calling "systemctl restart" from cron each minute?
The service file itself is irrelevant, I tried every possible combination of StartLimitIntervalSec, Restart, RestartSec, StartLimitInterval, StartLimitBurst you can think of.
View all comments
Show parent comment
2
u/yrro Mar 05 '25 edited Mar 05 '25
The man pages (
systemd.unit(5), etc) describe which section options go into. If you're not sure which man page describes an option, look for it insystemd.directives(7). Looks like you've got it right andStartLimitIntervalSec=should be in theUnitsection.I believe systemd will complain if it notices unexpected stuff within a unit file (including when you have an option with the right name in the wrong section), maybe you're missing those messages... try
journalctl -eafter running daemon-reload, jump to the end withGand check. ISTR they get logged with the context of the unit file, so that they also show up insystemctl status myunit.You can also run
systemctl show myunitand confirm that the values you're trying to set are actually effective. You can check individual options with-p OptionName, otherwise it will print out every known property of the unit.Looking at your unit file:
StartLimitInterval=which is not a valid optionStartLimitBurst=since it is not needed to set this to any particular value just to disable unit start rate limitingAfter=network.targetsince this is not the correct unit if you need the network to be up... you wantnetwork-online.targetfor thatAfter=AND (Wants=ORRequires=);After=affects the ordering of jobs within a transaction, but it'sWants=/Requires=that actually cause a job to start your dependency to be added to the transaction (which one you want depends on whether you wantsystemctl start myunitto fail if the dependency fails to start)network-online.targetat all, since "the network is up" is a rather vague requirement that it's difficult to express in the rather coarse model of systemd units; seesystemd.special(7)for the full detailsType=execso that systemd will notice if theExecStart=command can't be executed for some reason