Running a systemd service as a regular user
A "note to self" post on how to create a systemd service and run it a regular user.
Assuming, you're logged in as a regular user, create a .service file in /etc/systemd/user:
sudo nano /etc/systemd/user/my_service.service
Specify the service specifics:
[Unit]
Description=My_service
[Service]
Restart=always
ExecStart=/path/to/executable
ExecStop=/usr/bin/kill -HUP $MAINPID
[Install]
WantedBy=default.target
It's important that WantedBy
is set to default.target
.
Save the unit file, and reload the systemd daemon using the systemctl --user daemon-reload
command. Enable and start the service as the user:
systemctl --user enable my_service.service
systemctl --user start my_service.service
Finally, run the sudo loginctl enable-linger USER
command (replace USER with the actual user).