欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Linux中作业调度 crond 和 systemd.timer 的使用场景 - 一些笔记

最编程 2024-01-04 18:31:49
...
┌──[root@liruilongs.github.io]-[/etc/tmpfiles.d]
└─$man systemd.timer | cat
SYSTEMD.TIMER(5) systemd.timer SYSTEMD.TIMER(5)



NAME
systemd.timer - Timer unit configuration

SYNOPSIS
timer.timer

DESCRIPTION
# 以 ".timer" 为后缀的单元文件, 封装了一个由 systemd 管理的定时器, 以支持基于定时器的启动。
A unit configuration file whose name ends in ".timer" encodes information about a timer
controlled and supervised by systemd, for timer-based activation.

# 本手册列出了 所有专用于此类单元的配置选项(亦称"配置指令"或"单元属性")。
# systemd.unit(5) 中描述了通用于所有单元类型的配置选项, 它们位于 [Unit] 与 [Install] 小节。
# 此类单元专用的配置选项位于 [Timer] 小节。
This man page lists the configuration options specific to this unit type. See systemd.unit(5)
for the common options of all unit configuration files. The common configuration items are
configured in the generic [Unit] and [Install] sections. The timer specific configuration
options are configured in the [Timer] section.

# 每个定时器单元都必须有一个与其匹配的单元, 用于在特定的时间启动。
# 匹配的单元可以通过 Unit= 选项(见下文)明确指定。
# 若未指定,则默认是与该单元名称相同的 .service 单元(不算后缀)。例如 foo.timer 默认匹配 foo.service 单元。
For each timer file, a matching unit file must exist, describing the unit to activate when
the timer elapses. By default, a service by the same name as the timer (except for the
suffix) is activated. Example: a timer file foo.timer activates a matching service
foo.service. The unit to activate may be controlled by Unit= (see below).

# 注意,如果在启动时间点到来的时候,匹配的单元已经被启动, 那么将不执行任何动作,也不会启动任何新的服务实例。
# 因此,那些设置了 RemainAfterExit=yes(当该服务的所有进程全部退出之后,依然将此服务视为处于活动状态) 的服务单元一般不适合使用基于定时器的启动。
# 因为这样的单元仅会被启动一次,然后就永远处于活动(active)状态了。

# 下列依赖关系是自动隐含的:: 定时器单元自动获得对匹配单元的 Before= 依赖。
# 除非明确设置了 DefaultDependencies=no ,否则 timer 单元将会自动添加下列依赖关系:
# Requires=sysinit.target, After=sysinit.target, Before=timers.target 依赖; 以及 Conflicts=shutdown.target, Before=shutdown.target 依赖(确保在关机前干净的停止)。 只有那些在系统启动的早期就必须启动的定时器,以及那些必须在关机动作的结尾才能停止的定时器才需要设置 DefaultDependencies=no
Unless DefaultDependencies= is set to false, all timer units will implicitly have
dependencies of type Conflicts= and Before= on shutdown.target to ensure that they are
stopped cleanly prior to system shutdown. Timer units with at least one OnCalendar= directive
will have an additional After= dependency on timer-sync.target to avoid being started before
the system clock has been correctly set. Only timer units involved with early boot or late
system shutdown should disable the DefaultDependencies= option.

OPTIONS
# 定时器单元文件中必须包含一个 [Timer] 小节, 其中包含了该单元所定义的定时器的相关信息。 这里只列出仅能用于 [Timer] 小节的选项(亦称"指令"或"属性"):
Timer files must include a [Timer] section, which carries information about the timer it
defines. The options specific to the [Timer] section of timer units are the following:
# 定义相对于特定时间点之后一段时间的 单调定时器:
# OnActiveSec= 相对于该单元自身被启动的时间点;
# OnBootSec= 相对于机器被启动的时间点, 也就是内核开始运行的时间点;
# OnStartupSec= 相对于 systemd 被首次启动的时间点,也就是内核启动init进程的时间点;
# OnUnitActiveSec= 相对于匹配单元最后一次被启动的时间点;
# OnUnitInactiveSec= 相对于匹配单元 最后一次被停止的时间点;
OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=
Defines monotonic timers relative to different starting points: OnActiveSec= defines a
timer relative to the moment the timer itself is activated. OnBootSec= defines a timer
relative to when the machine was booted up. OnStartupSec= defines a timer relative to
when systemd was first started. OnUnitActiveSec= defines a timer relative to when the
unit the timer is activating was last activated. OnUnitInactiveSec= defines a timer
relative to when the unit the timer is activating was last deactivated.

# 可以组合使用这些指令(既可以将某个指令使用多次,也可以将多个指令一起使用),
# 例如,通过同时使用 OnBootSec= 与 OnUnitActiveSec= 指令, 就可以实现在系统启动后的某个时间点启动匹配单元, 并且之后每隔一段时间周期性的反复启动匹配单元。
Multiple directives may be combined of the same and of different types. For example, by
combining OnBootSec= and OnUnitActiveSec=, it is possible to define a timer that elapses
in regular intervals and activates a specific service each time.

# 指令的值是一个时间长度, 可以使用下面的时间单位后缀:us(微秒), ms(毫秒), s(秒), m(分), h(时), d(天), w(周) 。
# 如果省略了时间单位,那么表示使用默认单位"秒"。 可以同时使用多个时间单位,
# 例如 "OnBootSec=5h 30min" 表示系统启动之后的5小时30分钟。 更多有关表示时间长度的语法,参见 systemd.time(7) 手册。
The arguments to the directives are time spans configured in seconds. Example:
"OnBootSec=50" means 50s after boot-up. The argument may also include time units.
Example: "OnBootSec=5h 30min" means 5 hours and 30 minutes after boot-up. For details
about the syntax of time spans, see systemd.unit(5).

# 如果定时器单元在启动时已经超过了 OnBootSec= 或 OnStartupSec= 指定的时间, 那么将会立即启动匹配的单元。
# 但是对于使用其他指令定义的定时器, 超过了就等于错过了,不会尝试去补救。
If a timer configured with OnBootSec= or OnStartupSec= is already in the past when the
timer unit is activated, it will immediately elapse and the configured unit is started.
This is not the case for timers defined in the other directives.

# 这几个指令定义的定时器都是基于单调时间的单调定时器(monotonic timer),所谓"单调时间"的意思是从开机那一刻(零点)起, 只要系统正在运行,该时间就不断的单调均匀递增(但在系统休眠时此时间保持不变),永远不会往后退,并且与时区也没有关系。 即使在系统运行的过程中,用户向前/向后修改系统时间,也不会对"单调时间"产生任何影响。
These are monotonic timers, independent of wall-clock time and timezones. If the computer
is temporarily suspended, the monotonic clock stops too.

# 如果给某个指令赋予一个空字符串, 则表示 撤销该指令之前已设置的所有定时器
If the empty string is assigned to any of these options, the list of timers is reset, and
all prior assignments will have no effect.

# 注意, 这些指令设置的定时器并不必然在所设置的精准时间点上启动匹配单元, 因为它们还受到下文 AccuracySec= 选项的影响。
Note that timers do not necessarily expire at the precise time configured with these
settings, as they are subject to the AccuracySec= setting below.

OnCalendar=
# 定义基于挂钟时间(wallclock)的日历定时器,是与传统cron任务类似的定时器。详见 systemd.time(7) 手册以了解日历事件表达式的语法
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.
# 注意,该指令设置的定时器 并不必然在所设置的精准时间点上启动匹配单元, 因为它们还受到下文 AccuracySec= 选项的影响
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=
# 设置定时器的触发精度。默认值是一分钟。定时器并不必然在所设置的精准时间点上启动匹配单元, 而是在所设置的精准时
# 间点为起点的一小段时间窗口范围内的某个时间点上启动匹配单元, 这个时间窗口的起点由 OnCalendar=,
# OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec= 决定, 而这个时间窗口的长
# 度则由该指令决定。 在这个时间窗口内,触发点的具体位置虽然无法在系统开机之前预知, 也就是说触发点的具体位置在每
# 次启动之间是随机的, 但是一旦开机之后,对于正在运行中的系统而言又是固定的, 并且对全系统范围内所有的定时器而言
# 都是固定的。 这么做的目的在于避免过多不必要的CPU唤醒,以节约电力。 若想获得最高精度,可以将此选项设为"1us"(一
# 微秒),但是耗电量就可能明显增加。 注意,定时器的精度还受到 systemd-system.conf(5) 中的 TimerSlackNSec= 选
# 项的影响(不过一般可以忽略不计,参见 prctl(2) 手册)。 为了优化耗电量, 应该将此指令设为可接收的最大值。
Specify the accuracy the timer shall elapse with. Defaults to 1min. The timer is
scheduled to elapse within a time window starting with the time specified in OnCalendar=,
OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec= or OnUnitInactiveSec= and
ending the time configured with AccuracySec= later. Within this time window, the expiry
time will be placed at a host-specific, randomized, but stable position that is
synchronized between all local timer units. This is done in order to optimize power
consumption to suppress unnecessary CPU wake-ups. To get best accuracy, set this option
to 1us. Note that the timer is still subject to the timer slack configured via systemd-
system.conf(5)'s TimerSlackNSec= setting. See prctl(2) for details. To optimize power
consumption, make sure to set this value as high as possible and as low as necessary.'


RandomizedDelaySec=
# 将此单元的定时器随机延迟一小段时间, 这一小段时间的长度 介于零到该指令设置的时间长度之间, 以均匀概率分布。 默
# 认值是零,表示不延迟。 定时器单元在每次迭代前确定这个随机值的大小, 并简单的把它加到下一次启动匹配单元的时间点
# 上, 此选项可以将大量在同一时间点触发的定时器 均匀的分摊到一小段时间范围内触发, 以避免造成资源争抢。
# AccuracySec= 允许将一段时间内分散的定时器事件集中到一点,以减少CPU唤醒。 而本指令正好相反, 允许将集中在一点
# 的定时器事件分散到一小段时间范围内, 以避免造成资源争抢。如果同时使用 RandomizedDelaySec= 与 AccuracySec=
# 选项, 那么首先添加一个随机的时间, 然后结果可能进一步被平移, 以和系统上其他定时器事件合并。 因为默认设置
# AccuracySec=1min, RandomizedDelaySec=0 鼓励将多个定时器事件聚合在一起。 所以如果希望尽可能均匀分散定时器事
# 件,那么应该将 RandomizedDelaySec= 设为一个较大的值,同时设置 AccuracySec=1us

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 a randomized time is added, and the result is then
possibly shifted further to coalesce it with other timer events possibly 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=
# 该定时器单元的匹配单元, 也就是要被该定时器启动的单元。参数是一个不以 ".timer" 结尾的单元名。
# 默认值是 与此定时器单元同名的服务单元(见上文)。
# 建议将定时器单元的名字 与被该定时器启动的匹配单元的名字保持一致 (也就是仅单元后缀名不同)。
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=
# 此选项仅对 OnCalendar= 指令定义的日历定时器有意义。 若设为"yes",则表示将匹配单元的上次触发时间永久保存在磁盘
# 上。 这样,当定时器单元再次被启动时, 如果匹配单元本应该在定时器单元停止期间至少被启动一次, 那么将立即启动匹配
# 单元。 这样就不会因为关机而错过必须执行的任务。 默认值为 no
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=.

WakeSystem=
# 若设为"yes", 则表示当某个定时器到达触发时间点时, 唤醒正在休眠的系统并阻止系统进入休眠状态。
# 注意, 此选项仅确保唤醒系统, 而不关心任务执行完成之后是否需要再次休眠系统。 默认值为 no
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.

SEE ALSO
systemd(1), systemctl(1), systemd.unit(5), systemd.service(5), systemd.time(7),
systemd.directives(7), systemd-system.conf(5), prctl(2)



systemd 219 SYSTEMD.TIMER(5)
┌──[root@liruilongs.github.io]-[/etc/tmpfiles.d]
└─$