Is it possible to make systemd automatically restart a service only if the service did not return 0? As far as I have understood, a service returns 0, when a correct end occured- say when a user terminates the service actively like closing a program. I would love to tell systemd not to restart the service, when it was terminated by hand. Is that possible?

up vote 2 down vote accepted

Yes, you can use Restart=on-failure inside the unit's Service section.

From systemd's documentation (also available at man 5 systemd.service):

Restart=

[...]

If set to on-failure, the service will be restarted when the process exits with a non-zero exit code, is terminated by a signal (including on core dump, but excluding the aforementioned four signals), when an operation (such as service reload) times out, and when the configured watchdog timeout is triggered. If set to on-abnormal, the service will be restarted when the process is terminated by a signal (including on core dump, excluding the aforementioned four signals), when an operation times out, or when the watchdog timeout is triggered.

[...]

Setting this to on-failure is the recommended choice for long-running services, in order to increase reliability by attempting automatic recovery from errors. For services that shall be able to terminate on their own choice (and avoid immediate restarting), on-abnormal is an alternative choice.

As an aside, instead of your current method of terminating the process by hand, you can explicitly tell systemd to stop the service with systemctl stop, which will bypass any Restart= settings.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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