wiki/linux.systemd.timers.html

80 lines
17 KiB
HTML
Raw Normal View History

2022-07-29 15:41:17 +00:00
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width"/><meta charSet="utf-8"/><title>systemd timers</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async="" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><script type="text/x-mathjax-config">
MathJax = {
tex: {
inlineMath: [ [&#x27;$&#x27;,&#x27;$&#x27;], [&#x27;\(&#x27;,&#x27;\)&#x27;] ],
displayMath: [ [&#x27;$$&#x27;,&#x27;$$&#x27;], [&#x27;[&#x27;,&#x27;]&#x27;] ]
},
options = {
processHtmlClass = &quot;math&quot;
}
}
</script><meta name="next-head-count" content="6"/><link rel="preload" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" as="style"/><link rel="stylesheet" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" data-n-g=""/><noscript data-n-css=""></noscript><link rel="preload" href="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" as="script"/></head><body><div id="__next"><main><h1>systemd timers</h1><section class="post-metadata"><span>Date: <!-- -->2021-07-19 21:45:47 +08:00</span><span>Date modified: <!-- -->2022-05-22 22:28:49 +08:00</span></section><nav class="toc"><ol class="toc-level toc-level-1"><li class="toc-item toc-item-h1"><a href="/wiki/linux.systemd.timers#timer-management" class="toc-link toc-link-h1">Timer management</a></li><li class="toc-item toc-item-h1"><a href="/wiki/linux.systemd.timers#timedate-formats" class="toc-link toc-link-h1">Timedate formats</a></li></ol></nav><p>You can schedule tasks with timers.
If systemd is compiled with the feature, it makes cron unnecessary.
</p><h1 id="timer-management">Timer management</h1><p>In a fully-installed systemd-enabled system, there are multiple ways to manage your timers.
</p><p>While managing them is practically the same as any other units (see <a href="/wiki/linux.systemd">systemd</a>), there are timer-specific ways to manage them easier.
</p><ul><li><p><code class="inline-code">systemctl list-timers</code> is the go-to command that displays an overview of all active timers.
Just give the <code class="inline-verbatim">--all</code> flag to list all timers including disabled timers.
</p></li><li><p><code class="inline-code">systemctl status ${TIMER_UNIT}</code> is another way for a specific timer unit.
It gives the same details as the <code class="inline-verbatim">list-timers</code> subcommand so you&#x27;ll rarely use this subcommand in practice.
</p></li></ul><h1 id="timedate-formats">Timedate formats</h1><p>systemd has different ways to denote time.
</p><ul><li><p>Timespans denote the duration — e.g., <code class="inline-verbatim">100 seconds</code>, <code class="inline-verbatim">5M 3w</code>.
</p></li><li><p>Timestamps refer to a specific point in time — e.g., <code class="inline-verbatim">2021-04-02</code>, <code class="inline-verbatim">today</code>, <code class="inline-verbatim">now</code>.
</p></li><li><p>Calendar events can refer to more than one point of time — e.g., <code class="inline-verbatim">*-*-4/2</code>, <code class="inline-verbatim">Sun,Wed,Fri *-1/2-1/8</code>.
</p></li></ul><p>To find more details about time notation, you can view the <code class="inline-verbatim">systemd.time.7</code> manual page.
</p><p>Here&#x27;s an example of setting a timer for an example backup service.
The following timer unit sets it to execute every day at 18:00.
</p><pre class="src-block"><code class="language-ini">[Unit]
Description=A deduplicated backup from my computer
Documentation=man:borg(1) https://borgbackup.readthedocs.io/
[Timer]
Unit=borg-backup.service
OnCalendar=*-*-* 18:00:00
Persistent=true
[Install]
WantedBy=graphical.target
</code></pre><p>If the timer unit is started, this will trigger <code class="inline-verbatim">borg-backup.service</code> from the load path.
But you can omit <code class="inline-verbatim">Timer.Unit</code> key if you named the timer unit file similarly (e.g., <code class="inline-verbatim">borg-backup.timer</code> with <code class="inline-verbatim">borg-backup.service</code>).
</p><p>You can find more information about it from the <code class="inline-verbatim">systemd.timer.5</code> manual page.
Furthermore, systemd has a testing tool for time with <code class="inline-code">systemd-analyze {timespan,timestamp,calendar}</code>.
</p><pre class="src-block"><code class="language-shell">printf &quot;Timespan example:\n&quot;
printf &quot;..............\n&quot;
systemd-analyze timespan 4000min
printf &quot;..............\n\n&quot;
printf &quot;Timestamp example:\n&quot;
printf &quot;..............\n&quot;
systemd-analyze timestamp 2021-07-01
printf &quot;..............\n\n&quot;
printf &quot;Calendar example:\n&quot;
printf &quot;..............\n&quot;
systemd-analyze calendar &quot;*-1/4-5 0/2:00:00&quot;
printf &quot;..............\n\n&quot;
</code></pre><div class="exampe">Timespan example:
..............
Original: 4000min
μs: 240000000000
Human: 2d 18h 40min
..............
Timestamp example:
..............
Original form: 2021-07-01
Normalized form: Thu 2021-07-01 00:00:00 PST
(in UTC): Wed 2021-06-30 16:00:00 UTC
UNIX seconds: @1625068800
From now: 10 months 21 days ago
..............
Calendar example:
..............
Original form: *-1/4-5 0/2:00:00
Normalized form: *-01/4-05 00/2:00:00
Next elapse: Mon 2022-09-05 00:00:00 PST
(in UTC): Sun 2022-09-04 16:00:00 UTC
From now: 3 months 13 days left
..............
</div><section><h2>Backlinks</h2><ul><li><a href="/wiki/linux.systemd">systemd</a></li><li><a href="/wiki/linux.systemd.transient-units">systemd transient units</a></li></ul></section></main></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"metadata":{"date":"2021-07-19 21:45:47 +08:00","date_modified":"2022-05-22 22:28:49 +08:00","language":"en","source":""},"title":"systemd timers","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"timer-management"},"children":[{"type":"text","value":"Timer management"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/linux.systemd.timers#timer-management"},"children":[{"type":"text","value":"Timer management"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"timedate-formats"},"children":[{"type":"text","value":"Timedate formats"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/linux.systemd.timers#timedate-formats"},"children":[{"type":"text","value":"Timedate formats"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can schedule tasks with timers.\nIf systemd is compiled with the feature, it makes cron unnecessary.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"timer-management"},"children":[{"type":"text","value":"Timer management"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a fully-installed systemd-enabled system, there are multiple ways to manage your timers.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While managing them is practically the same as any other units (see "},{"type":"element","tagName":"a","properties":{"href":"/linux.systemd"},"children":[{"type":"text","value":"systemd"}]},{"type":"text","value":"), there are timer-specific ways to manage them easier.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemctl list-timers"}]},{"type":"text","value":" is the go-to command that displays an overview of all active timers.\n Just give the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"--all"}]},{"type":"text","value":" flag to list all timers including disabled timers.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemctl status ${TIMER_UNIT}"}]},{"type":"text","value":" is another way for a specific timer unit.\n It gives the same details as the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"list-timers"}]},{"type":"text","value":" subcommand so you'll rarely use this subcommand in practice.\n"}]}]}]},{"type":"element","tagName":"h1","properties":{"id":"timedate-formats"},"children":[{"type":"text","value":"Timedate formats"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"systemd has different ways to denote time.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timesp