In an earlier post we learned how to start a daemon with systemd. But it is annoying to handle a user daemon this way: type every time a
sudo in front of the
systemctl command. Let’s configure it, so that it can be used as a normal user.
To enable systemd for a user, root has to allow this:
# loginctl enable-linger maker
maker is the username here). This is a one-time action for a specific user. After root has done this, the user can use systemd for his or her own daemons.
The configuration files are not in
/etc/systemd/system (of course, because only root is allowed to write there), but in
~/.config/systemd/user/. So let’s create a file
sensor.target in this directory:
[Unit] Description=sensornet main daemon After=network.target [Service] Environment=CONFIG=/home/maker/projects/sensors/sensors.cfg WorkingDirectory=/home/maker/projects/sensornet ExecStart=/home/maker/projects/sensornet/bin/collect.py Restart=always RestartSec=5 [Install] WantedBy=default.target
All the rest is handled with the usual systemd commands with a little detail: you have to add the
--user flag to the commands:
$ systemctl --user enable sensor $ systemctl --user start sensor $ systemctl --user status sensor
Additionally the user daemon is started at boot time.