149 lines
5.6 KiB
Markdown
149 lines
5.6 KiB
Markdown
---
|
||
title: Creating User’s 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, we’ll learn to install, manage and control services on a per-user basis.
|
||
|
||
## 2\. Creating and Adding a Sample Service
|
||
|
||
First, let’s 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, let’s prepare the [service’s 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, we’ve commented them out. Next, we redirect the script’s output to *log_file* with the *StandardOutput* entry. It’s worth noting that the *%h* modifier stands for the user’s home directory.
|
||
|
||
**Now, with the [sudo](https://www.baeldung.com/linux/sudo-command) privilege let’s copy the unit file to the */etc/systemd/user* directory.** In this way, *systemd* regards the service as the user’s 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, let’s install the service:
|
||
|
||
```bash
|
||
systemctl --user daemon-reload
|
||
```
|
||
|
||
Afterwards, let’s start it:
|
||
|
||
```bash
|
||
systemctl --user start user_service.service
|
||
```
|
||
|
||
Next, let’s 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 we’ve done that all without the *sudo* command.
|
||
|
||
## 4\. Enabling, Disabling, and the Service’s Lifetime[](#enabling-disabling-and-the-services-lifetime)
|
||
|
||
Now let’s 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, it’ll 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, let’s 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 Service’s Life
|
||
|
||
Let’s 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 Service’s Log
|
||
|
||
Now let’s check the service’s log with the [*journalctl*](https://www.baeldung.com/linux/journalctl-check-logs) command. Once again, we’ll 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 service’s log.
|