Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure a systemd service to restart periodically?

I have a simple systemd service that needs to be periodically restarted to keep its process from bugging out. Is there a configuration option for systemd services to periodically restart them? All of the Restart* options seem to pertain to restarting the service when it exits.

like image 744
wes Avatar asked Jun 25 '15 15:06

wes


People also ask

Can a systemd service restart itself?

No, there is not. This is a hard limitation imposed by systemd, and a substantial breaking change to the expectation of how processes work.

How do I restart a Linux service automatically?

After reloading the systemd manager configuration ( sudo systemctl daemon-reload ), kill your running service and confirm that it is automatically restarted after the time specified by RestartSec has elapsed. Thanks for reading!

What is RestartSec?

RestartSec= Configures the time to sleep before restarting a service (as configured with Restart= ). Takes a unit-less value in seconds, or a time span value such as "5min 20s". Defaults to 100ms.


2 Answers

For systemd version >= 229, there is an option called RuntimeMaxSec, which terminates the service after it has been running for the given period of time.

e.g.

[Service] Restart=always RuntimeMaxSec=604800 

To me this seems more elegant than abusing Type=notify and WatchdogSec.

systemd provides a clean way to add and override directives in systemd unit files provided by vendors. Drop-In Units are described in man systemd.unit. For example, if you wanted to periodically restart the foo service provided by a package, you would create a file named /etc/systemd/system/foo.service.d/periodic-restart.conf. The contents would be as shown above. Then:

 systemctl daemon-reload  systemctl restart foo 

You can confirm that the Drop-In unit has been loaded because it will be reported in the status output:

 systemctl status 

Finally, you can confirm the directive has been included by searching the systemctl show output:

 systemctl show foo.service | grep RuntimeMax 

The directive reported by systemctl show will be "RuntimeMaxUSec`

like image 97
Alex Forbes Avatar answered Sep 27 '22 22:09

Alex Forbes


I saw a solution here that seemed elegant, if a bit roundabout. The key idea is to create a one-shot service triggered by a timer that restarts another service.

For the timer:

[Unit] Description=Do something daily  [Timer] OnCalendar=daily Persistent=true  [Install] WantedBy=timers.target 

For the one-shot service:

[Unit] Description=Restart service  [Service] Type=oneshot ExecStart=/usr/bin/systemctl try-restart my_program.service 

For the one-shot service on Ubuntu 16.04 LTS:

[Unit] Description=Restart service  [Service] Type=oneshot ExecStart=/bin/systemctl try-restart my_program.service 

This solution lets you leverage systemd's timers, including the ability to restart the service at a particular time of day, and not just after some amount of time has elapsed.

like image 24
matmat Avatar answered Sep 27 '22 22:09

matmat