0

I've made a systemd service with a timer that should run the script every hour, but it's not running every hour.

% cat /etc/systemd/system/myservice.service
[Unit]
Description=Run script after network established
Requires=network-online.target
After=network-online.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart="/srv/script"

[Install]
WantedBy=multi-user.target

% cat /etc/systemd/system/myservice.timer
[Unit]
Description=Run script every hour

[Timer]
OnBootSec=15min
OnUnitActiveSec=1h

[Install]
WantedBy=timers.target

It does run the script after the network is established, but it doesn't continue to run every hour after that.

sudo systemctl status myservice.{service,timer}

...shows that both are active and enabled.

How do I make the time file reference the service file?


More info:

% systemctl list-timers --system --all
NEXT                        LEFT         LAST                        PASSED       UNIT                         ACTIVATES
~~~~~~~~~~~~~~~~
Sun 2023-09-24 03:10:54 BST 3 days left  Sun 2023-09-17 03:10:50 BST 3 days ago   e2scrub_all.timer            e2scrub_all.service
-                           -            -                           -            fstrim.timer                 fstrim.service
-                           -            Wed 2023-09-20 17:28:10 BST 2h 27min ago myservice.timer              myservice.service
5
  • What does systemctl list-timers --system --all return? Commented Sep 20, 2023 at 18:55
  • @ajgringo619: I've added the relevant output. There are no dates in the first two columns.
    – paradroid
    Commented Sep 20, 2023 at 18:59
  • Check out the differences in systemctl cat fstrim.{service,timer}. For the service, remove the [install] section; for the timer, add Persistent=True under the [Timer] section. Then reload with systemctl daemon-reload. Commented Sep 20, 2023 at 20:56
  • @ajgringo619 Removing the [Install] section doesn't seem to make any improvement but adding Persistent=True makes the timer run after one hour once, and then nothing after that.
    – paradroid
    Commented Sep 21, 2023 at 5:31
  • Hmmm..maybe the RemainAfterExit=yes entry is the culprit. Commented Sep 21, 2023 at 6:41

1 Answer 1

1

man systemd.timer says:

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.

You are using RemainAfterExit=yes which causes the service to remain active, even after /srv/script has completed its work. Becuase your service is active, the timer does not spawn another instance.

The answer is probably to remove RemainAfterExit=yes. At the very least, you need to make sure the service is stopped if you want the timer to trigger anything.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .