大网数据广告活动
服务器运维问答

Running systemd-run --user inside a systemd service

· 52CE 我爱测

I have a web app that handles user input, and as part of that, runs some commands with systemd-run --user --scope ... to limit memory and CPU usage. The application runs fine when run normally, but when run as a systemd service, …

问题原文

原帖链接 · user2141650 · CC BY-SA 4.0

I have a web app that handles user input, and as part of that, runs some commands with systemd-run --user --scope ... to limit memory and CPU usage.

The application runs fine when run normally, but when run as a systemd service, I get:

Failed to connect to bus: No medium found

What do I need to do in the unit for the service to enable this?

已采纳解答

原帖链接 · u1686_grawity · CC BY-SA 4.0

--user contacts the per-user instance of systemd for that UID, which is only started when there is an actual login session for that UID. When there aren't any, you need to loginctl enable-linger <user> to have the service manager be always running for that user.

The webapp needs to have the correct XDG_RUNTIME_DIR= in its environment so that it could find the bus socket; it's always at the same location /run/user/<uid>. (The directory will be created by systemd when linger is enabled.)

You will probably need to use --service instead of --scope. The latter won't be able to migrate your process between cgroups due to lack of "common ancestor" permissions, so it becomes necessary to have systemd itself start the process.

(In a user session, your own per-UID user-1234.slice cgroup is the common ancestor of both your interactive terminal cgroup and the new systemd scope cgroup, but when trying to do this from a service, the only common ancestor would be -.slice which you do not have permissions to.)


Configuring the webapp .service with a PAMName= so that the webapp actually runs in a login session would avoid some of the above problems, but I also have the feeling it'll cause new problems, so I'm not suggesting it as the first option.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-02-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。