vault backup: 2025-12-10 11:37:35

This commit is contained in:
2023-05-15 17:16:05 +02:00
committed by Thomas Peetz
parent 91bf72fc87
commit 73f2162ddf
6049 changed files with 513094 additions and 227748 deletions
@@ -0,0 +1,148 @@
---
title: Creating Users Services With systemd | Baeldung on Linux
source: https://www.baeldung.com/linux/systemd-create-user-services
tags:
- IT/Administration
- IT/OS/Linux
---
## 1\. Overview
Most Linux distributions use [*systemd*](https://www.baeldung.com/linux/create-remove-systemd-services) 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:
```bash
#!/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](https://man7.org/linux/man-pages/man5/systemd.unit.5.html) *user_service.service*:
```properties
[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](https://www.baeldung.com/linux/sudo-command) 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*](https://www.baeldung.com/linux/differences-systemctl-service#systemctlcommand). Thus, services can be enabled/disabled, started/stopped, and so on without the sudo privilege.
So, lets install the service:
```bash
systemctl --user daemon-reload
```
Afterwards, lets start it:
```bash
systemctl --user start user_service.service
```
Next, lets check its status:
```bash
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[](#enabling-disabling-and-the-services-lifetime)
Now lets enable or disable the service with *systemctl*:
```bash
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:
```bash
systemctl --user disable user_service.service
```
## 5\. Toggling Services of All Users[](#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*:
```bash
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*](https://man7.org/linux/man-pages/man1/loginctl.1.html) with the *enable-linger* command:**
```bash
$ 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*](https://www.baeldung.com/linux/journalctl-check-logs) command. Once again, well use the *user* option:
```bash
$ 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.