systemd.timer,是由systemd提供的定时任务,类似于Cron功能。
简单描述
文件命名
该单元文件以.timer为文件后缀。单元文件命名为<timer>.timer。其中,<timer>为具体的任务名,而.timer为固定的文件后缀。
文件结构
本文不涉及单元文件的[Unit]部分与[Install]部分,有关内容参考systemd.unit(5)手册。
本文只包含[Timer]部分的指令,主要摘自systemd.tiemr(5)手册。特定于定时器的配置选项都位于单元文件的[Timer]部分中。
# 其他方面
对于每个定时器文件,必须存在匹配的单元文件,描述了计时器经过时要激活的单元文件。默认情况下,激活与计时器同名的服务(除了后缀)。例如,计时器foo.timer将执行foo.service服务。需要指定要执行的单元,通过Unit=指令来指定。
!!!在计时器经过时,要激活的任务已经激活,它不会被重新开始,而是仍然保持运行。在这种情况下不会产生新的实例。因此服务中使用了RemainAfterExit=(即使主进程退出了也保持存在状态)不适合通过计时器来激活,因为他们只会被激活一次,然后一直存在,服务不退出就无法进行下一次任务。
自动依赖
隐含依赖
定时器单元会在它们应该激活的服务上自动获得一个Before=依赖。
默认依赖
除非设置了DefaultDependencies=no,否则会添加以下依赖:
命令支持的选项及含义
计时器文件必须包含一个[Timer]部分,该部分包含了计时器的定义。特定于[Timer]部分的选项如下:
OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=
定义相对于不同起点的单调计时器:
多个指令可以由相同和不同类型组合,在这种情况下,只要任何指定的计时器表达式过去,计时器单元就会触发。 例如,通过组合OnBootSec=和OnUnitActiveSec=,可以定义一个定期过去的计时器,并且每次都激活一个特定的服务。 此外,单调时间表达式和OnCalendar=日历表达式可以在同一计时器单元中组合。
指令的参数是以秒为单位配置的时间跨度。 示例:“OnBootSec=50”表示启动后50秒。 参数还可以包括时间单位。 示例:“OnBootSec=5h 30min”表示启动后5小时30分钟。有关时间跨度语法的详细信息,请参阅systemd.time(7)。
如果在激活定时器单元时,配置了OnBootSec=或OnStartupSec=的定时器已经过去,它将立即经过并启动配置的单元。对于其他指令中定义的计时器,情况并非如此。
这些是单调计时器,与时间和时区无关。 如果计算机暂时休眠,单调时钟也会休眠。
如果将空字符串分配给这些选项中的任何一个,则重置定时器列表(单调定时器和OnCalendar=定时器,见下文),并且所有先前的分配都不起作用。
请注意,定时器不一定会在使用这些设置配置的精确时间到期,因为它们受以下AccuracySec =设置的限制。
OnCalendar=
information on the syntax of calendar event expressions. Otherwise, the semantics are similar to
OnActiveSec= and related settings.
subject to the AccuracySec= setting below.
AccuracySec=
指定计时器经过的时间精度。计时器计划在一个时间窗口内经过,该时间窗口从Calendar=, OnActiveSec=, OnBootSec=,
OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=指定的时间开始,在AccuracySec=配置的时间时候结束。在时间窗口中,过期时间是主机特定、随机的,但是“在本地计时器单元之间同步”的固定位置。之所以这么做是为了优化功耗,减少不必要的CPU唤醒。如果为了获取更好的精确度,将该选项设置为1us。
注意:计时器仍然受到“计时器松弛”的影响,计时器松弛是通过systemd-system.conf(5)的TimerSlackNSec=选项配置。参考prctl(2)了解更多细节。要优化功耗,请确保将此值设置为尽可能高且必要时尽可能低。
默认:1min
RandomizedDelaySec=
time value. Defaults to 0, indicating that no randomized delay shall be applied. Each timer unit will
determine this delay randomly each time it is started, and the delay will simply be added on top of the
next determined elapsing time. This is useful to stretch dispatching of similarly configured timer
events over a certain amount time, to avoid that they all fire at the same time, possibly resulting in
resource congestion. Note the relation to AccuracySec= above: the latter allows the service manager to
coalesce timer events within a specified time range in order to minimize wakeups, the former does the
opposite: it stretches timer events over a time range, to make it unlikely that they fire
simultaneously. If RandomizedDelaySec= and AccuracySec= are used in conjunction, first the randomized
delay is added, and then the result is possibly further shifted to coalesce it with other timer events
happening on the system. As mentioned above AccuracySec= defaults to 1min and RandomizedDelaySec= to 0,
thus encouraging coalescing of timer events. In order to optimally stretch timer events over a certain
range of time, make sure to set RandomizedDelaySec= to a higher value, and AccuracySec=1us.
Unit=
If not specified, this value defaults to a service that has the same name as the timer unit, except for
the suffix. (See above.) It is recommended that the unit name that is activated and the unit name of the
timer unit are named identically, except for the suffix.
Persistent=
When the timer is activated, the service unit is triggered immediately if it would have been triggered
at least once during the time when the timer was inactive. This is useful to catch up on missed runs of
the service when the machine was off. Note that this setting only has an effect on timers configured
with OnCalendar=. Defaults to false.
WakeSystem=
should it be suspended and if the system supports this. Note that this option will only make sure the
system resumes on the appropriate times, it will not take care of suspending it again after any work
that is to be done is finished. Defaults to false.
RemainAfterElapse=
If false, an elapsed timer unit that cannot elapse anymore is unloaded. Turning this off is particularly
useful for transient timer units that shall disappear after they first elapse. Note that this setting
has an effect on repeatedly starting a timer unit that only elapses once: if RemainAfterElapse= is on,
it will not be started again, and is guaranteed to elapse only once. However, if RemainAfterElapse= is
off, it might be started again if it is already elapsed, and thus be triggered multiple times. Defaults
to yes.
相关手册
systemd(1), systemctl(1), systemd.unit(5), systemd.service(5), systemd.time(7), systemd.directives(7), systemd-system.conf(5), prctl(2)
参考文献
man 5 systemd.timer, Version systemd 232
systemd.timer — Timer unit configuration