66

I'm trying to make a systemd timer that runs every 15 minutes. Right now I have:

  • timer-fifteen.timer:

    [Unit]
    Description=15min timer
    
    [Timer]
    OnBootSec=0min
    OnCalendar=*:*:0,15,30,45
    Unit=timer-fifteen.target
    
    [Install]
    WantedBy=basic.target  
    
  • timer-fifteen.target:

    [Unit]
    Description=15min Timer Target
    StopWhenUnneeded=yes
    

This runs over and over again without stopping. Does it need to be *:0,15,30,45:* instead? How can I make this work?

Timur Shtatland
  • 448
  • 2
  • 9
Lily Hahn
  • 763
  • 1
  • 5
  • 5

2 Answers2

93

Your syntax translates to every 15 seconds, if you want every 15 minutes, IMO the most readable way is:

OnCalendar=*:0/15

An answer most similar to what you use in your question is:

OnCalendar=*:0,15,30,45

More information:

Pavel Šimerda
  • 6,394
  • 2
  • 26
  • 33
  • 6
    `OnCalendar=0/8:00:00` is correct for saying "run every 8 hours"? – shackra Dec 26 '15 at 20:01
  • 4
    Yes. But you probably already know that by now. :) – Pavel Šimerda Jan 05 '16 at 12:47
  • I didn't, but I used that anyway lol. Thank you for confirming! – shackra Jan 07 '16 at 03:06
  • `OnCalendar=*:0/15` should translate to `OnCalendar=*:0,15,30,45` exactly (given I read it correctly in the docs). Every 8 hours would be realised with `OnUnitActiveSec=8hours`. – Debilski Jul 06 '16 at 10:53
  • @PavelŠimerda where can we find more information on this syntax(I mean the *:h/m)? I checked on the link https://www.freedesktop.org/software/systemd/man/systemd.time.html but I couldn't find much info regarding *:h/m syntax. – Dinesh P.R. Aug 29 '16 at 04:59
  • 1
    @DineshP.R. It is there. – “In the date and time specifications, any component may be specified as "*" in which case any value will match. Alternatively, each component can be specified as a list of values separated by commas. Values may also be suffixed with "/" and a repetition value, which indicates that the value itself and the value plus all multiples of the repetition value are matched. Each component may also contain a range of values separated by "..".” – Pavel Šimerda Sep 01 '16 at 09:35
  • 16
    When specifying an interval, the output of `systemd-analyze` may come in handy, for example try `systemd-analyze calendar *:0/15`. – sebastian Mar 05 '18 at 08:28
  • @sebastian systemd as recent as version 234 does not have a `calendar` subcommand for `systemd-analyze`. What version are you using? – larsks Mar 24 '18 at 01:27
  • systemd 238 on Debian GNU/Linux buster/sid can do `systemd-analyze calendar *:0/15`. Edit: the `calendar` verb has been introduced in 236, see https://launchpad.net/ubuntu/+source/systemd/+changelog. – sebastian Mar 24 '18 at 07:54
  • Can't we use `15min` instead of that? – Zeta.Investigator Sep 10 '21 at 10:44
29

According to the systemd.time, the setting

OnCalendar=*:0/15

translates exactly to

OnCalendar=*:0,15,30,45

ie. it activates the unit exactly at the full hour, as well as at quarter past, half past and quarter to.

Depending on your service, this may not be what you want, nor what you need in all cases.

A timer that runs every 15 minutes – for example at 1:02, 1:17, 1:32, 1:47, 2:02, … – always depending on the time it was last run – can be accomplished with the systemd.timer setting

OnUnitActiveSec=15min

Now, you will also want the unit to start some time after boot (unless you want to activate the unit manually or have a dependency which does that), so you should maybe specify

OnBootSec=10min
OnUnitActiveSec=15min

to have the unit started 10 minutes after boot and then every 15 minutes after that first time.

Additionally, there is the setting OnUnitInactiveSec which starts counting the time after the service has stopped (or, more generally, the unit has inactivated).

Debilski
  • 443
  • 5
  • 7
  • 7
    Warning: `OnUnitActiveSec` [seems to be pretty broken](https://github.com/systemd/systemd/issues/6680). – Anders Kaseorg Jan 24 '19 at 19:51
  • Note that this is a monotonic timer -- it is relative to system uptime and not to real time. If your system slept every 5 minutes for 5 minutes, a 15 minute monotonic timer would run every 25-30 minutes. – Codebling Feb 20 '23 at 20:34