「systemd.timer」 @20210202

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,否则会添加以下依赖:

 

计时器会自动获得在 sysinit.target上的 Requires=After=的依赖,一个 timers.target上的 Before=类型的依赖,以及 shutdown.target上的 Conflicts=Before=依赖(以确保系统在关闭期间能干净的停止)。只有涉及早期启动或延迟系统关闭的计时器单元才应禁用 DefaultDependencies=选项。

 

 

对于具有至少一个 OnCalendar=指令的定时器单元,将在 time-sync.target上具有额外的After=依赖,以避免“在正确设置系统时钟之前”启动。

 

命令支持的选项及含义

计时器文件必须包含一个[Timer]部分,该部分包含了计时器的定义。特定于[Timer]部分的选项如下:

OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=
定义相对于不同起点的单调计时器:

 

OnActiveSec=:定义计时器,相对于计时器单元本身被激活的时刻。

 

 

OnBootSec=:定义计时器,相对于机器启动的时刻。 在容器中,对于系统管理器实例,它映射到OnStartupSec=,这两者是相同的。

 

 

OnStartupSec=:定义计时器,相对于首次启动服务管理器的时刻。对于系统计时器单元,这与OnBootSec=非常相似,因为系统服务管理器通常在启动时很早就启动。在以每用户服务管理器运行的单元中配置时,它主要是有用的,因为用户服务管理器通常仅在首次登录时启动,而不是在引导期间启动。

 

 

OnUnitActiveSec=:定义一个计时器,该计时器相对于上次激活计时器单元激活的单元。

 

 

OnUnitInactiveSec=:定义一个计时器,该计时器相对于上次取消激活计时器单元激活的单元。

 

多个指令可以由相同和不同类型组合,在这种情况下,只要任何指定的计时器表达式过去,计时器单元就会触发。 例如,通过组合OnBootSec=和OnUnitActiveSec=,可以定义一个定期过去的计时器,并且每次都激活一个特定的服务。 此外,单调时间表达式和OnCalendar=日历表达式可以在同一计时器单元中组合。

指令的参数是以秒为单位配置的时间跨度。 示例:“OnBootSec=50”表示启动后50秒。 参数还可以包括时间单位。 示例:“OnBootSec=5h 30min”表示启动后5小时30分钟。有关时间跨度语法的详细信息,请参阅systemd.time(7)。

如果在激活定时器单元时,配置了OnBootSec=或OnStartupSec=的定时器已经过去,它将立即经过并启动配置的单元。对于其他指令中定义的计时器,情况并非如此。

这些是单调计时器,与时间和时区无关。 如果计算机暂时休眠,单调时钟也会休眠。

如果将空字符串分配给这些选项中的任何一个,则重置定时器列表(单调定时器和OnCalendar=定时器,见下文),并且所有先前的分配都不起作用。

请注意,定时器不一定会在使用这些设置配置的精确时间到期,因为它们受以下AccuracySec =设置的限制。

OnCalendar=

Defines realtime (i.e. wallclock) timers with calendar event expressions. See systemd.time(7) for more
information on the syntax of calendar event expressions. Otherwise, the semantics are similar to
OnActiveSec= and related settings.

 

 

Note that timers do not necessarily expire at the precise time configured with this setting, as it is
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=

Delay the timer by a randomly selected, evenly distributed amount of time between 0 and the specified
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=

The unit to activate when this timer elapses. The argument is a unit name, whose suffix is not ".timer".
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=

Takes a boolean argument. If true, the time when the service unit was last triggered is stored on disk.
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=

Takes a boolean argument. If true, an elapsing timer will cause the system to resume from suspend,
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=

Takes a boolean argument. If true, an elapsed timer will stay loaded, and its state remains queriable.
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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值