Simple Systemd User Service Example
Problem
Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)
Create systemd unit file
Create a systemd unit file using the example below, give it a name that will make sense to you with a .service
extension (like [my_service]@.service), and save it to /lib/systemd/system/
, let’s assume your username is ubuntu
.
[Unit]
Description=my-service
After=network.target
[Service]
Type=exec
ExecStart=/your-start-script/run.sh
Restart=always
User=%i
[Install]
WantedBy=default.target
Enable and Start
sudo systemctl daemon-reload
sudo systemctl enable [my_service]@ubuntu.service
sudo systemctl start [my_service]@ubuntu.service
sudo systemctl status [my_service]@ubuntu.service
journalctl -u [my_service]@ubuntu.service
Reference
https://blog.victormendonca.com/2018/05/14/creating-a-simple-systemd-user-service/
https://www.baeldung.com/linux/systemd-create-user-services
https://wiki.archlinux.org/title/Systemd/User
https://nts.strzibny.name/systemd-user-services/