Restart systemd .service with .timer unit

servicessystemdsystemd-timersystemd-unit

I have the following systemd myscript.service unit that executes an script:

[Unit]
Description=MyScript
Wants=time-sync.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/test.sh -a
ExecStop=/usr/local/bin/test.sh -b

[Install]
WantedBy=multi-user.target

And I want to restart it each minute with the following Systemd myscript.timer unit:

[Unit]
Description=Schedule script execution

[Timer]
OnCalendar=*:0/1

[Install]
WantedBy=timers.target

It only works the first time, after it, the status remains in n/a:

# systemctl list-timers
NEXT                         LEFT        LAST                         PASSED      UNIT                         ACTIVATES
n/a                          n/a         Tue 2019-10-15 14:50:01 EDT  3ms ago     myscript.timer               myscript.service

~

Best Answer

If a .service unit will be executed from a .timer unit, it can't have RemainAfterExit=true. The systemd documentation mentions that:

Note that in case the unit to activate is already active at the time the timer elapses it is not restarted, but simply left running. There is no concept of spawning new service instances in this case. Due to this, services with RemainAfterExit= set (which stay around continuously even after the service's main process exited) are usually not suitable for activation via repetitive timers, as they will only be activated once, and then stay around forever.

So, remove RemainAfterExit=true and it will work.