Systemd/timers/zh: Difference between revisions
Created page with "定时器是 systemd 中名称以 .timer 结尾的单元文件,用于控制 .service 文件或事件。定时器可以用作 <code>cron</code> 的替代方案。定时器内置对基于日历的事件和普通时间事件的支持,并且可以异步运行。" |
Created page with "== 配置 ==" |
||
| (One intermediate revision by the same user not shown) | |||
| Line 4: | Line 4: | ||
<span id="Configuration"></span> | <span id="Configuration"></span> | ||
== 配置 == | == 配置 == | ||
以下示例定时器每 5 分钟运行一次 systemd 单元以调用 bash 脚本。 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
systemd.timers."hello-world" = { | systemd.timers."hello-world" = { | ||
| Line 92: | Line 90: | ||
====Running timer on a schedule==== | ====Running timer on a schedule==== | ||
以下示例每天启动一次(凌晨 12:00)。激活后,例如如果由于系统关闭错过了上次启动时间(同时设置选项 Persistent=true),,则会立即触发服务。 | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
... | ... | ||
| Line 103: | Line 99: | ||
}; | }; | ||
</syntaxHighlight> | </syntaxHighlight> | ||
更多示例可以在 [https://wiki.archlinux.org/title/Systemd/Timers Arch Wiki] 和 <code>systemd.timer</code> 手册页中找到。 | |||
<span id="Usage"></span> | <span id="Usage"></span> | ||
== 用法 == | == 用法 == | ||
列出活跃定时器及其当前状态: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl list-timers | systemctl list-timers | ||
</syntaxhighlight> | </syntaxhighlight> | ||
为了测试目的,手动运行一次服务: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl start hello-world | systemctl start hello-world | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:systemd]] | [[Category:systemd]] | ||