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?
Yes, you can use
Restart=on-failure inside the unit's
From systemd's documentation (also available at
man 5 systemd.service):
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-failureis 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-abnormalis 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