{{tag>systemd}} ====== systemd user service ====== 1. Create user service in user's home directory ex. ///home//.config/systemd/user/// Example: [Unit] Description=my npm app After=network.target [Service] ExecStart=/usr/bin/npm start WorkingDirectory=/home/debian/mynpmapp/ Restart=on-failure [Install] WantedBy=default.target 2. Enable service systemctl --user enable mynpmapp.service 3. From root enable lingering so that it persists over reboots and logouts loginctl enable-linger username 4. Use ''systemctl --user start|stop '' to run service ===== TROUBLESHOOTING ===== Below is only needed if you switch to the user from root or another user on the system. If you login via ssh as a user, PAM subsystem will set up the required variables. ==== systemd --user does not work: 'Failed to connect to bus' ==== apt install dbus-user-session In .bashrc of the user put: export XDG_RUNTIME_DIR=/run/user/$(id -u) export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" then logout and login again. If its still not working and you get na error Failed to connect to bus: No such file or directory try rebooting the server. ====== Tested on ====== * Debian 10 ====== See also ====== ====== References ====== * https://wiki.archlinux.org/index.php/Systemd/User#Automatic_start-up_of_systemd_user_instances * https://serverfault.com/questions/841099/systemd-grant-an-unprivileged-user-permission-to-alter-one-specific-service * https://serverfault.com/a/1047069/353235 * https://unix.stackexchange.com/questions/434494/how-do-i-setup-user-autostart-and-properly-configure-systemd-user-services/434647#434647