mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-01-31 01:57:54 +00:00
80 lines
17 KiB
HTML
80 lines
17 KiB
HTML
<!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: [ ['$','$'], ['\(','\)'] ],
|
|
displayMath: [ ['$$','$$'], ['[',']'] ]
|
|
},
|
|
options = {
|
|
processHtmlClass = "math"
|
|
}
|
|
}
|
|
</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'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'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 "Timespan example:\n"
|
|
printf "..............\n"
|
|
systemd-analyze timespan 4000min
|
|
printf "..............\n\n"
|
|
|
|
printf "Timestamp example:\n"
|
|
printf "..............\n"
|
|
systemd-analyze timestamp 2021-07-01
|
|
printf "..............\n\n"
|
|
|
|
printf "Calendar example:\n"
|
|
printf "..............\n"
|
|
systemd-analyze calendar "*-1/4-5 0/2:00:00"
|
|
printf "..............\n\n"
|
|
</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":"Timespans denote the duration — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"100 seconds"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"5M 3w"}]},{"type":"text","value":".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timestamps refer to a specific point in time — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"2021-04-02"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"today"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"now"}]},{"type":"text","value":".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calendar events can refer to more than one point of time — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"*-*-4/2"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Sun,Wed,Fri *-1/2-1/8"}]},{"type":"text","value":".\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To find more details about time notation, you can view the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"systemd.time.7"}]},{"type":"text","value":" manual page.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's an example of setting a timer for an example backup service.\nThe following timer unit sets it to execute every day at 18:00.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-ini"]},"children":[{"type":"text","value":"[Unit]\nDescription=A deduplicated backup from my computer\nDocumentation=man:borg(1) https://borgbackup.readthedocs.io/\n\n[Timer]\nUnit=borg-backup.service\nOnCalendar=*-*-* 18:00:00\nPersistent=true\n\n[Install]\nWantedBy=graphical.target\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the timer unit is started, this will trigger "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"borg-backup.service"}]},{"type":"text","value":" from the load path.\nBut you can omit "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Timer.Unit"}]},{"type":"text","value":" key if you named the timer unit file similarly (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"borg-backup.timer"}]},{"type":"text","value":" with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"borg-backup.service"}]},{"type":"text","value":").\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can find more information about it from the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"systemd.timer.5"}]},{"type":"text","value":" manual page.\nFurthermore, systemd has a testing tool for time with "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemd-analyze {timespan,timestamp,calendar}"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"printf \"Timespan example:\\n\"\nprintf \"..............\\n\"\nsystemd-analyze timespan 4000min\nprintf \"..............\\n\\n\"\n\nprintf \"Timestamp example:\\n\"\nprintf \"..............\\n\"\nsystemd-analyze timestamp 2021-07-01\nprintf \"..............\\n\\n\"\n\nprintf \"Calendar example:\\n\"\nprintf \"..............\\n\"\nsystemd-analyze calendar \"*-1/4-5 0/2:00:00\"\nprintf \"..............\\n\\n\"\n"}]}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"Timespan example:\n..............\nOriginal: 4000min\n μs: 240000000000\n Human: 2d 18h 40min\n..............\n\nTimestamp example:\n..............\n Original form: 2021-07-01\nNormalized form: Thu 2021-07-01 00:00:00 PST\n (in UTC): Wed 2021-06-30 16:00:00 UTC\n UNIX seconds: @1625068800\n From now: 10 months 21 days ago\n..............\n\nCalendar example:\n..............\n Original form: *-1/4-5 0/2:00:00\nNormalized form: *-01/4-05 00/2:00:00\n Next elapse: Mon 2022-09-05 00:00:00 PST\n (in UTC): Sun 2022-09-04 16:00:00 UTC\n From now: 3 months 13 days left\n..............\n\n"}]}]},"backlinks":[{"path":"/linux.systemd","title":"systemd"},{"path":"/linux.systemd.transient-units","title":"systemd transient units"}]},"__N_SSG":true},"page":"/[[...slug]]","query":{"slug":["linux.systemd.timers"]},"buildId":"Ie9t5zutrXP6Of75Cb5xF","assetPrefix":"/wiki","nextExport":false,"isFallback":false,"gsp":true}</script><script nomodule="" src="/wiki/_next/static/chunks/polyfills-99d808df29361cf7ffb1.js"></script><script src="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" async=""></script><script src="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" async=""></script><script src="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" async=""></script><script src="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" async=""></script><script src="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" async=""></script><script src="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" async=""></script><script src="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" async=""></script><script src="/wiki/_next/static/Ie9t5zutrXP6Of75Cb5xF/_buildManifest.js" async=""></script><script src="/wiki/_next/static/Ie9t5zutrXP6Of75Cb5xF/_ssgManifest.js" async=""></script></body></html> |