How to Use Systemd to Restart a Service When Down

How to use Systemd to restart a service when down?

If you are using a systemd service file to start your service, then add the lines below to your service file from where you are starting your service:

[Service]
Type=simple
ExecStart=here will be your service executable name
Restart=always
RestartSec=0
  • Restart=

    Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached. Takes one of the following values: no, on-success, on-failure, on-abnormal, on-watchdog, on-abort or always. If set to no (the default).

  • RestartSec=

    Configures the time to sleep before restarting a service (as configured with Restart=). Takes a unit-less value in seconds.

These two options have to be under the [Service] tag in a service file.

How can I configure a systemd service to restart periodically?

Yes, you can make your service to restart it periodically by making your service of Type=notify.
Add this option in [Service] section of your service file along with Restart=always and give WatchdogSec=xx, where xx is the time period in second you want to restart your service. Here your process will be killed by systemd after xx time period and will be restarted by systemd again.
for eg.

[Unit]
.
.

[Service]
Type=notify
.
.
WatchdogSec=10
Restart=always
.
.

[Install]
WantedBy= ....

How to restart a service if its dependent service is restarted

You can use PartOf.

[Unit]
After=foo.service
Requires=foo.service
PartOf=foo.service

From the systemd.unit man page:

PartOf=

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.

Automatically restart a service after unattended updates under systemd

Use Restart=always if you want it to run at all times. When mysql service update happens, this service does a clean stop and therefore systemd doesn't restart it. You have Restart=on-failure set, which only restarts if the stop has a return code other than 0.

Restart = always
RestartSec = 10

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.

How to set systemd to be sure that it stops my application on server shutdown/reboot

Thanks @kamilcuk for the link i made some changes my final configuration file looks like this:

[Unit]
Description=My Application
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/my_app start
ExecReload=/usr/local/bin/my_app reload
ExecStop=/usr/local/bin/my_app stop
TimeoutStopSec=10
# KillMode=mixed

[Install]
WantedBy=multi-user.target

I've disable and remove all links in /etc/systemd/ before re enable it, so everything looks good on shutdown and reboot.

Restart service when service file changes when using Ansible

There are two solutions.

Register + When changed

You can register template module output (with its status change),

register: service_conf

and then use when clause.

when: service_conf.changed

For example:

---
- name: Systemd service
template:
src: sonar.unit.j2
dest: /etc/systemd/system/sonarqube.service
when: "ansible_service_mgr == 'systemd'"
register: service_conf

- name: restart service
service:
name: sonarqube
state: restarted
when: service_conf.changed

Handler + Notify

You define your restart service task as handler. And then in your template task you notify the handler.

tasks:
- name: Add Sonarqube to Systemd service
template:
src: sonar.unit.j2
dest: /etc/systemd/system/sonarqube.service
when: "ansible_service_mgr == 'systemd'"
notify: Restart Sonarqube
- …

handlers:
- name: Restart Sonarqube
service:
name: sonarqube
state: restarted

More info can be found in Ansible Doc.

Difference between those 2?

In the first case, the service will restart directly. In the case of the handler the restart will happen at the end of the play.

Another difference will be, if you have several tasks changes that need to restart of your service, you simply add the notify to all of them.

  • The handler will run if any of those task get a changed status. With the first solution, you will have to register several return. And it will generate a longer when clause_1 or clause_2 or
  • The handler will run only once even if notified several times.

User systemd service restarting only when SSH-ing into the machine

I found the solution to my problem there. I had misunderstood the behavior of the --user flag (VS using the User= property in the service file)

I was running under debian 11 and as stated in the mentioned answer, my service would not necessarily shut down after logging out of ssh, but only at some point (not clear if it happened when my service crashed for the first time or some sort of garbage collection)

And the service would boot up again magically when SSHing in the instance as a reaction to a user login and starting all the services.

So the fix was to reimplement the services using User= and without the --user flag to make it a globally available service.

Do you know how to make the system automatically restart daemon service?

To respawn your service when it fails, add the following to the [Service] block:

[Service]
Restart=on-failure
RestartSec=3

If you wish to always restart when your service is killed use Restart=always

The RestartSec value is the delay between restart attempts.

See more info here: https://www.freedesktop.org/software/systemd/man/systemd.service.html



Related Topics



Leave a reply



Submit