Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run timer exactly every second with systemd

Tags:

How to run some service exactly every second? Overlapping allowed.

Here is my timer unit for systemd:

[Unit]
Description=Send jobs every second

[Timer]
OnBootSec=1min
OnUnitActiveSec=1s
Unit=app.jobs-send.service

[Install]
WantedBy=multi-user.target

But app.jobs-send.service service doesn't executed every second:

May 07 17:57:38 app.deployer.org systemd[1]: Started Send jobs to queue.
May 07 17:57:48 app.deployer.org systemd[1]: Starting Send jobs to queue...
May 07 17:57:48 app.deployer.org systemd[1]: Started Send jobs to queue.
May 07 17:57:58 app.deployer.org systemd[1]: Starting Send jobs to queue...
May 07 17:57:58 app.deployer.org systemd[1]: Started Send jobs to queue.
May 07 17:58:08 app.deployer.org systemd[1]: Starting Send jobs to queue...
May 07 17:58:08 app.deployer.org systemd[1]: Started Send jobs to queue.

There is a gap about 17:57:58 - 17:57:48 = 10 seconds.

How to exec task every second?

like image 913
Anton Medvedev Avatar asked May 07 '18 18:05

Anton Medvedev


People also ask

How do I run a systemd timer?

To start the timer, use the command sudo systemctl start helloworld. timer . To enable the timer on boot, run the command sudo systemctl enable helloworld.

How does systemd timer work?

Systemd timers offer the best of both cron and anacron. Allows scheduling down to minute granularity. Assures that the task will be executed when the system is again running even if it was off during the expected execution time. Is available to all users.

How do I see systemd timers?

To list all timers (including inactive), use systemctl list-timers --all . The status of a service started by a timer will likely be inactive unless it is currently being triggered.


1 Answers

As documented in man systemd.time, the accuracy of OnUnitActiveSec= is subject to the AccuracySec= setting, also documented there. Set to AccuracySec=1us for best resolution.

like image 53
Mark Stosberg Avatar answered Oct 01 '22 03:10

Mark Stosberg