wiki/linux.systemd.services.html

58 lines
20 KiB
HTML
Raw Permalink 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 services</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 services</h1><section class="post-metadata"><span>Date: <!-- -->2021-07-19 21:46:52 +08:00</span><span>Date modified: <!-- -->2021-08-02 11:56:12 +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.services#service-management-basics" class="toc-link toc-link-h1">Service management basics</a></li><li class="toc-item toc-item-h1"><a href="/wiki/linux.systemd.services#service-configuration" class="toc-link toc-link-h1">Service configuration</a></li></ol></nav><p>One of the functions of the system suite is service management.
Like most of the components, it can be used at user-level with their set locations, managing the service daemon, and all.
</p><h1 id="service-management-basics">Service management basics</h1><p>Just plop down a service unit file at one of the search paths and you can start managing right away.
For more information, see the manual page (i.e., <code class="inline-verbatim">systemd.service.5</code>).
A summarized version can be found at <a href="/wiki/Service%20configuration">Service configuration</a>.
</p><p>Here&#x27;s an example of a user service resided as <code class="inline-verbatim">$HOME/.config/systemd/user/drive-backup.service</code>.
</p><pre class="src-block"><code class="language-ini">[Unit]
Description=Periodic safety backup for my external drive
Documentation=man:borg(1) https://www.borgbackup.org/ https://torsion.org/borgmatic/
[Service]
Type=oneshot
ExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 create
ExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 prune
ExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 check
[Install]
WantedBy=default.target
</code></pre><p>You can then start the service with:
</p><pre class="src-block"><code class="language-shell">systemctl --user start drive-backup.service
</code></pre><p>You can also stop it with the <code class="inline-verbatim">stop</code> subcommand (e.g., <code class="inline-code">systemctl --user stop drive-backup.service</code>) and restart it with <code class="inline-verbatim">restart</code> (e.g., <code class="inline-code">systemctl --user restart drive-backup.service</code>).
</p><p>If you want to enable it at startup, you can go with <code class="inline-verbatim">enable</code> subcommand.
(To disable it, use the <code class="inline-verbatim">disable</code> subcommand.)
</p><pre class="src-block"><code class="language-shell">systemctl --user enable drive-backup.service
</code></pre><p>systemd will use the configuration file as-is by the time it is started/enabled.
Which means if the config file has been modified after activation, it will not take effect until you restarted it.
For this, you can reload the daemon with <code class="inline-verbatim">daemon-reload</code> subcommand.
But for simpler cases, you can use the <code class="inline-verbatim">reload</code> subcommand without fully restarting the daemon.
</p><pre class="src-block"><code class="language-shell">systemctl --user reload drive-backup.service
# You could also use...
# systemctl --user daemon-reload
# ...if you need a stronger option.
</code></pre><h1 id="service-configuration">Service configuration</h1><p>There are different types of services.
</p><ul><li><p>The most common type of service is <code class="inline-verbatim">simple</code> which considers the unit active after the main process is forked (e.g., <code class="inline-verbatim">Service.ExecStart</code>).
This is the recommended type for long-running processes.
</p></li><li><p><code class="inline-verbatim">oneshot</code> marks the service resolved after the main process exits.
Due to the behavior, it will directly go from activating to deactivating instead of active.
</p></li><li><p><code class="inline-verbatim">exec</code> considers the service active after the binary has been executed.
</p></li></ul><p>Aside from types, each service may have one or more commands although the behavior is set depending on the type.
</p><ul><li><p><code class="inline-verbatim">ExecStart</code> which is usually the main command and most services will throw an error if it&#x27;s missing.
All services, unless specified as a <code class="inline-verbatim">oneshot</code> service, only have one of these values.
</p></li><li><p><code class="inline-verbatim">ExecStop</code> only executes after the main command successfully starts.
</p></li><li><p><code class="inline-verbatim">ExecStartPre</code> and <code class="inline-verbatim">ExecStartPost</code> gives you additional commands that will be executed before and after the main command, respectively.
</p></li><li><p><code class="inline-verbatim">ExecStopPre</code> and <code class="inline-verbatim">ExecStopPost</code> is similar to the pre- and post-start commands except for the stop command.
</p></li><li><p><code class="inline-verbatim">Reload</code> sets whether the service restarts on fail.
Values accepted are <code class="inline-verbatim">no</code>, <code class="inline-verbatim">on-failure</code>, and <code class="inline-verbatim">on-success</code>.
</p></li></ul><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:46:52 +08:00","date_modified":"2021-08-02 11:56:12 +08:00","language":"en","source":""},"title":"systemd services","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":"service-management-basics"},"children":[{"type":"text","value":"Service management basics"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/linux.systemd.services#service-management-basics"},"children":[{"type":"text","value":"Service management basics"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"service-configuration"},"children":[{"type":"text","value":"Service configuration"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/linux.systemd.services#service-configuration"},"children":[{"type":"text","value":"Service configuration"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the functions of the system suite is service management.\nLike most of the components, it can be used at user-level with their set locations, managing the service daemon, and all.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"service-management-basics"},"children":[{"type":"text","value":"Service management basics"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just plop down a service unit file at one of the search paths and you can start managing right away.\nFor more information, see the manual page (i.e., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"systemd.service.5"}]},{"type":"text","value":").\nA summarized version can be found at "},{"type":"element","tagName":"a","properties":{"href":"/Service%20configuration"},"children":[{"type":"text","value":"Service configuration"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's an example of a user service resided as "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"$HOME/.config/systemd/user/drive-backup.service"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-ini"]},"children":[{"type":"text","value":"[Unit]\nDescription=Periodic safety backup for my external drive\nDocumentation=man:borg(1) https://www.borgbackup.org/ https://torsion.org/borgmatic/\n\n[Service]\nType=oneshot\nExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 create\nExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 prune\nExecStart=%h/.nix-profile/bin/borgmatic --config %h/dotfiles/borgmatic/personal-drive.yaml --verbosity 2 check\n\n[Install]\nWantedBy=default.target\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can then start the service with:\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"systemctl --user start drive-backup.service\n"}]}