site/wiki/pages/user-units.md

47 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2018-09-07 16:43:10 +00:00
---
author: ~ben
published: true
title: daemonize with user units
description: a quick tutorial on creating and managing daemonized processes with systemd user units
2022-09-29 18:53:09 +00:00
category:
- guides
- technical
2018-09-07 16:43:10 +00:00
---
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!
1. ensure that your user unit loadpath is set up:
2019-08-04 20:39:27 +00:00
mkdir -p ~/.config/systemd/user/
2018-09-07 16:43:10 +00:00
2022-09-29 18:53:09 +00:00
2. 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)
2019-08-04 20:39:27 +00:00
[Unit]
Description=foo
2018-09-07 16:43:10 +00:00
2019-08-04 20:39:27 +00:00
[Service]
ExecStart=/bin/bash -c "while true do; echo hi; done"
2018-09-07 16:43:10 +00:00
2019-08-04 20:39:27 +00:00
[Install]
WantedBy=default.target
2018-09-07 16:43:10 +00:00
2022-09-29 15:14:01 +00:00
3. enable it
2019-08-04 20:39:27 +00:00
systemctl --user enable --now my-new-service.service
2018-09-07 16:43:10 +00:00
2022-09-29 15:14:01 +00:00
4. enable-linger for your user account
2019-08-04 20:39:27 +00:00
loginctl enable-linger
2018-09-07 16:43:10 +00:00
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)