Files
thpeetz-notes/Quellen/IT/Creating User’s Services With systemd.md
T

5.6 KiB
Raw Blame History

title, source, tags
title source tags
Creating Users Services With systemd | Baeldung on Linux https://www.baeldung.com/linux/systemd-create-user-services
IT/Administration
IT/OS/Linux

1. Overview

Most Linux distributions use systemd as a contemporary service manager. Usually, we need to become root to control the services. However, we can also allow ordinary users to handle services on their own. This method is often called rootless.

In this tutorial, well learn to install, manage and control services on a per-user basis.

2. Creating and Adding a Sample Service

First, lets write a simple Bash script user_service for the service:

#!/bin/bash

while true
do
    now=$(date)
    me=$(whoami)
    echo "User $me at $now"
    sleep 10
done

So, the script prints the date and user name. Then, we need to copy the script to the /usr/local/bin directory and make sure that the user has executable permission on it.

Next, lets prepare the services unit file user_service.service:

[Unit]
Description=Script Daemon For Test User Services

[Service]
Type=simple
#User=
#Group=
ExecStart=/usr/local/bin/user_service
Restart=on-failure
StandardOutput=file:%h/log_file

[Install]
WantedBy=default.target 

Since the entries User and Group are meaningless for user service, weve commented them out. Next, we redirect the scripts output to log_file with the StandardOutput entry. Its worth noting that the %h modifier stands for the users home directory.

Now, with the sudo privilege lets copy the unit file to the /etc/systemd/user directory. In this way, systemd regards the service as the users one. Moreover, the service is available for all users.

3. The user Option

We can manage services as regular users with the help of the user option of systemctl. Thus, services can be enabled/disabled, started/stopped, and so on without the sudo privilege.

So, lets install the service:

systemctl --user daemon-reload

Afterwards, lets start it:

systemctl --user start user_service.service

Next, lets check its status:

systemctl --user status user_service.service
● user_service.service - Script Daemon For Test User Services
     Loaded: loaded (/etc/xdg/systemd/user/user_service.service; disabled; vendor preset: enable>
     Active: active (running) since Thu 2023-01-12 19:23:14 CET; 28s ago
   Main PID: 4935 (user_service)
      Tasks: 2 (limit: 18982)
     Memory: 580.0K
        CPU: 16ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/user_service.service
             ├─4935 /bin/bash /usr/local/bin/user_service
             └─4972 sleep 10

sty 12 19:23:14 ubuntu systemd[1511]: Started Script Daemon For Test User Services.

Again, we should note that weve done that all without the sudo command.

4. Enabling, Disabling, and the Services Lifetime

Now lets enable or disable the service with systemctl:

systemctl --user enable user_service.service
Created symlink /home/joe/.config/systemd/user/default.target.wants/user_service.service → /etc/xdg/systemd/user/user_service.service.

Thus, once enabled, the service starts automatically after our login. Then, itll be running as long as we have some open sessions. In other words, the service instance is bound to the user, not to the session.

Finally, lets disable it:

systemctl --user disable user_service.service

5. Toggling Services of All Users

With the root privilege, we can enable or disable the service for all users with the global option of systemctl:

sudo systemctl --global enable user_service.service
Created symlink /etc/systemd/user/default.target.wants/user_service.service → /etc/systemd/user/user_service.service.

Consequently, all users obtain their own running instance of the service immediately after login.

6. Extending Services Life

Lets assume that we intend our service to perform long-running tasks, e.g., calculation. So, we should extend its life beyond the end of the last session. In that case, we use loginctl with the enable-linger command:

$ loginctl enable-linger

Now, our services start right after the system boot and run till the shutdown. We should keep in mind that this applies to all our systemd services. Finally, we can turn it off with disable-linger.

7. Inspecting Services Log

Now lets check the services log with the journalctl command. Once again, well use the user option:

$ journalctl --user -u user_service
# ...
Jan 12 19:50:20 ubuntu systemd[1511]: Stopped Script Daemon For Test User Services.
Jan 12 19:50:24 ubuntu systemd[1511]: Started Script Daemon For Test User Services.

8. Conclusion

In this article, we looked at user services managed by systemd.

First, we created a simple service and added it to the systemd using the administrator privilege. Then, we managed the service as a non-sudoer thanks to the user option of systemctl.

Subsequently, we took a look at the lifetime of service and learned how to start the service without login. Finally, we examined the services log.