wiki/_next/data/Ie9t5zutrXP6Of75Cb5xF/linux.systemd.unit-templates.json

1 line
3.9 KiB
JSON
Raw Permalink Normal View History

2022-07-29 15:41:17 +00:00
{"pageProps":{"metadata":{"date":"2021-08-07 19:55:05 +08:00","date_modified":"2021-08-07 19:55:26 +08:00","language":"en","source":""},"title":"systemd unit templates","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":"p","properties":{},"children":[{"type":"text","value":"You can create unit templates which is useful for simple services that only requires an argument.\nRather than creating individual simple service files, let systemd handle it.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, you may want to spawn a service for Borgmatic with multiple repos.\nIf you don't know templates, the dumb way to serve multiple repos is to create individual unit files for each.\nIf you want to schedule them, you also have to create a timer unit for each.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The more efficient solution is to use templates.\nTo make a unit template, there are only a handful of requirements:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Addition of "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"%i"}]},{"type":"text","value":" to represent the template value.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The unit file name has to end with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"@"}]},{"type":"text","value":" (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"unit-name@.service"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"unit-name@.timer"}]},{"type":"text","value":").\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This could be compressed into a template for a service unit.\nThe following code shows how to create one.\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 %i\nDocumentation=man:borg(1) https://www.borgbackup.org/\n\n[Service]\nType=simple\nExecStart=borgmatic --config %i --verbose\n\n[Install]\nWantedBy=default.target\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To use the service, you have to give it a value — e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"systemctl --user start borg-backup@test.yaml.service"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That's all good but what about scheduling them?\nWhat if you want to create an archive every hour starting at 08:00?\nYou can just create a templated timer unit.\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 %i\nDocumentation=man:borg(1) https://www.borgbackup.org/\n\n[Timer]\nUnit=borg-backup@%i.service\nCalendar=08/1:00:00\nPersistent=true\n\n[Install]\nWantedBy=default.target\n"}]}]}]},"backlinks":[{"path":"/linux.systemd","title":"systemd"}]},"__N_SSG":true}