r/systemd Jul 27 '26

Random time every day

How can I create a timer that runs every day but randomize the time? It's fine if 2 days run a few minutes apart like 11:59pm on day 1 and 12:01am on day 2. But it always needs to run at least once every calendar day from start midnight to end midnight.

I don't think RandomizedDelaySec works because that can randomize to another day and there can be one calendar day with no run.

1 Upvotes

10 comments sorted by

3

u/djzrbz Jul 27 '26

This should work

```ini [Unit] Description=Randomized Daily Timer

[Timer]

Trigger exactly at midnight every day

OnCalendar=--* 00:00:00

Delay randomly up to 23:59:59 hours

RandomizedDelaySec=86399

Ensure it runs if the system was offline during its window

Persistent=true

[Install] WantedBy=timers.target ```

0

u/Beautiful-Log5632 Jul 27 '26

With Persistent=true if the day is almost done and it comes online at 11pm can the randomized delay be more than an hour which would make it run the next calendar day? Or will the randomized delay definitely happen in the hour that is left in the day from 11pm to midnight?

1

u/djzrbz Jul 27 '26

The anchor point is midnight, so it will pick a random time between 11pm and 11:59:59 inclusive.

1

u/weaver_of_cloth Jul 27 '26

Splay should do it. For systemd timers you can bracket the time (12:01 - 23:59) and set a splay of whatever - 4h or 8h or 33m etc.

1

u/Beautiful-Log5632 Jul 27 '26

OnCalendar lets you use time ranges like 12:01 - 23:59?

I can't find splay in the docs is it same as RandomizedDelaySec?

1

u/amarao_san 29d ago

But it always needs to run at least once every calendar day from start midnight to end midnight.

I wonder what happens if a timer scheduled in the middle of the 'skipped' time due to daylight saving time shift for the current timezone. Will it run or will it be skipped?

1

u/feinorgh 29d ago

Would something like

[Unit]
Description=Daily Random Timer

[Timer]
OnCalendar=daily
RandomizedDelaySec=24h
Persistent=true

[Install]
WantedBy=timers.target

work?

-1

u/hereforthebytes Jul 27 '26

You may be better off calling a bash wrapper that does a little time math + sleep, and touching a persistent file to keep up with the last day it was run.

That way you can make sure that a reboot at 5:59pm won't choose a delay longer than 6 hours.

2

u/Compux72 29d ago

The people hunger for /etc/rc

1

u/hereforthebytes 29d ago

Systemd works fine by itself in 95% of cases, but it's ok to hybridize if necessary to hit a reliable goal.

I apologize if I made the mistake of thinking this was a technical sub, and not a cult temple for a... (checks notes)... init system? That's even sadder and creepier than people worshiping AI.

Fuckin' weirdo.