Systemd/timers/zh: Difference between revisions

Ardenet (talk | contribs)
Created page with "Systemd/定时器"
 
Ardenet (talk | contribs)
Created page with "== 配置 =="
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Systemd/breadcrumb}}
{{Systemd/breadcrumb}}


<div lang="en" dir="ltr" class="mw-content-ltr">
定时器是 systemd 中名称以 .timer 结尾的单元文件,用于控制 .service 文件或事件。定时器可以用作 <code>cron</code> 的替代方案。定时器内置对基于日历的事件和普通时间事件的支持,并且可以异步运行。
Timers are systemd unit files whose name ends in .timer that control .service files or events. Timers can be used as an alternative to <code>cron</code>. Timers have built-in support for calendar-based events and monotonic time events, and can be run asynchronously.
<span id="Configuration"></span>
</div>
== 配置 ==
<div lang="en" dir="ltr" class="mw-content-ltr">
以下示例定时器每 5 分钟运行一次 systemd 单元以调用 bash 脚本。
== Configuration ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
The following example timer runs a systemd unit every 5 minutes which invokes a bash script.
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
systemd.timers."hello-world" = {
systemd.timers."hello-world" = {
Line 95: Line 90:
====Running timer on a schedule====
====Running timer on a schedule====


<div lang="en" dir="ltr" class="mw-content-ltr">
以下示例每天启动一次(凌晨 12:00)。激活后,例如如果由于系统关闭错过了上次启动时间(同时设置选项 Persistent=true),,则会立即触发服务。
The following example starts once a day (at 12:00am). When activated, it triggers the service immediately if it missed the last start time (option Persistent=true), for example due to the system being powered off.
</div>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
...
...
Line 106: Line 99:
};
};
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
更多示例可以在 [https://wiki.archlinux.org/title/Systemd/Timers Arch Wiki] <code>systemd.timer</code> 手册页中找到。
More examples can be found at the [https://wiki.archlinux.org/title/Systemd/Timers Arch Wiki] and at the <code>systemd.timer</code> manpage.
<span id="Usage"></span>
</div>
== 用法 ==
<div lang="en" dir="ltr" class="mw-content-ltr">
列出活跃定时器及其当前状态:
== Usage ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
List active timers and their current state:
</div>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
systemctl list-timers
systemctl list-timers
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
为了测试目的,手动运行一次服务:
Manually run a service once for testing purposes:
</div>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
systemctl start hello-world
systemctl start hello-world
</syntaxhighlight>
</syntaxhighlight>
[[Category:systemd]]
[[Category:systemd]]