1.5 KiB
1.5 KiB
author | published | title | description | category | |
---|---|---|---|---|---|
~ben | true | daemonize with user units | a quick tutorial on creating and managing daemonized processes with systemd user units |
|
daemonize all the things
so you've got a process that you want to keep running. you might have it in a tmux or screen session. let's use systemd user units to manage it!
-
ensure that your user unit loadpath is set up:
mkdir -p ~/.config/systemd/user/
-
create a basic service. save something like this in
~/.config/systemd/user/my-new-service.service
(using the name of the unit you're creating)[Unit] Description=foo [Service] ExecStart=/bin/bash -c "while true; do echo hi; sleep 1; done" [Install] WantedBy=default.target
-
set the
XDG_RUNTIME_DIR
andDBUS_SESSION_BUS_ADDRESS
environment variables if they were missingexport XDG_RUNTIME_DIR="/run/user/$UID" export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"
-
enable and start it
systemctl --user enable --now my-new-service.service
-
enable-linger for your user account
loginctl enable-linger
this allows your user units to run even when you're not logged in.
done!
you can now use systemctl --user
to manage your daemonized process.
pro-tip: add alias sysu='systemctl --user'
to your shell's configuration
for a handy shortcut (or any other alias as you choose)