Systemd service files
I decided to write this quick n dirty recipe, as I always have to make a few web searches to remind myself on how to do a Linux systemd service file.
Very basically a service file is a set of instructions on how to execute a script/program. Useful parameters such as when the script should start, what do to if there are errors, etc.
- Location: service files are usually located in /etc/systemd/system/
- extension: have the extension .service
`man systemd.unit` provides an extensive list of the different options which can be used in a service unit, but I'll list a few common ones:
[Unit]generic options, such as description, unit's behavior, and dependencies
- Description A short description of the unit.
- Documentation A list of URIs referencing documentation.
- Before, After The order in which units are started.
- Requires If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or fails, this unit will be deactivated.
- Wants Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies.
- Conflicts If a unit has a Conflicts setting on another unit, starting the former will stop the latter and vice versa.
- Alias A space-separated list of additional names for the unit. Most systemctl commands, excluding systemctl enable, can use aliases instead of the actual unit name.
- RequiredBy, WantedBy The current service will be started when the listed services are started. See the description of Wants and Requires in the [Unit] section for details.
- Also Specifies a list of units to be enabled or disabled along with this unit when a user runs systemctl enable or systemctl disable.
After creating your myservice.service file:
- enable it: sudo systemctl enable myservice.service
- the command with create a symbolic link
- start the service: sudo systemctl start myservice.service
- check its status: sudo systemctl satus myservice.service
If after enabled you make changes to the service file, you will need to
sudo systemctl daemon-reload to ingrate the changes.
Example: jackd service run as USER
Tweaked according to: https://github.com/systemd/systemd/issues/2690
- location: /etc/systemd/user/jack.service
WantedBy=default.targetas there is no multi-user.target in --user mode
- enabled linger:
sudo loginctl enable-linger "$USER"to make a user specific systemd instance persistent, so that users can run unattended long running services see link
Enable / Start:
systemctl --user enable jack.service
systemctl --user start jack.service
- is your service wanted by default.target? Is it present in
systemctl --user list-dependencies default.target?
systemctl --user status
[Unit] Description=jack After=sound.target Reboot and run debug commands again. [Service] ExecStart=/usr/bin/jackd -d alsa -d hw:1 --rate 48000 ExecReload=/usr/bin/jackd -d alsa -d hw:1 --rate 48000 TimeoutStopSec=10 Restart=always RestartSec=30 [Install] WantedBy=default.target