mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-04-29 12:19:22 +00:00
1 line
11 KiB
JSON
1 line
11 KiB
JSON
{"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"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also stop it with the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"stop"}]},{"type":"text","value":" subcommand (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemctl --user stop drive-backup.service"}]},{"type":"text","value":") and restart it with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"restart"}]},{"type":"text","value":" (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemctl --user restart drive-backup.service"}]},{"type":"text","value":").\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to enable it at startup, you can go with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"enable"}]},{"type":"text","value":" subcommand.\n(To disable it, use the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"disable"}]},{"type":"text","value":" subcommand.)\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"systemctl --user enable drive-backup.service\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"systemd will use the configuration file as-is by the time it is started/enabled.\nWhich means if the config file has been modified after activation, it will not take effect until you restarted it.\nFor this, you can reload the daemon with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"daemon-reload"}]},{"type":"text","value":" subcommand.\nBut for simpler cases, you can use the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"reload"}]},{"type":"text","value":" subcommand without fully restarting the daemon.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"systemctl --user reload drive-backup.service\n\n# You could also use...\n# systemctl --user daemon-reload\n# ...if you need a stronger option.\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"service-configuration"},"children":[{"type":"text","value":"Service configuration"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are different types of services.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The most common type of service is "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"simple"}]},{"type":"text","value":" which considers the unit active after the main process is forked (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Service.ExecStart"}]},{"type":"text","value":").\n This is the recommended type for long-running processes.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"oneshot"}]},{"type":"text","value":" marks the service resolved after the main process exits.\n Due to the behavior, it will directly go from activating to deactivating instead of active.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"exec"}]},{"type":"text","value":" considers the service active after the binary has been executed.\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Aside from types, each service may have one or more commands although the behavior is set depending on the type.\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-verbatim"]},"children":[{"type":"text","value":"ExecStart"}]},{"type":"text","value":" which is usually the main command and most services will throw an error if it's missing.\n All services, unless specified as a "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"oneshot"}]},{"type":"text","value":" service, only have one of these values.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"ExecStop"}]},{"type":"text","value":" only executes after the main command successfully starts.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"ExecStartPre"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"ExecStartPost"}]},{"type":"text","value":" gives you additional commands that will be executed before and after the main command, respectively.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"ExecStopPre"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"ExecStopPost"}]},{"type":"text","value":" is similar to the pre- and post-start commands except for the stop command.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Reload"}]},{"type":"text","value":" sets whether the service restarts on fail.\n Values accepted are "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"no"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"on-failure"}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"on-success"}]},{"type":"text","value":".\n"}]}]}]}]},"backlinks":[{"path":"/linux.systemd","title":"systemd"},{"path":"/linux.systemd.transient-units","title":"systemd transient units"}]},"__N_SSG":true} |