如果有一个名为位于的alice
用户级别 systemd 服务单元,那么我可以将用户切换到 alice 并使用该开关检查该单元的状态:foo.service
/home/alice/.config/systemd/user/foo.service
--user
root@srv0:~# su - alice
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
alice@srv0:~$ systemctl status foo.service --user
● foo.service - foo
Loaded: loaded (/home/alice/.config/systemd/user/foo.service; disabled; v
Active: active (running) since Sat 2019-03-23 04:11:03 UTC; 2min 6s ago
Main PID: 2246 (python3)
CGroup: /user.slice/user-20021.slice/[email protected]/foo.service
└─2246 python3 /home/alice/devapp foo bar baz
是否可以在不切换用户的情况下检查设备状态(并操作设备)?
我尝试过使用sudo
但它不起作用:
root@srv0:~# sudo -l -u alice systemctl status --user foo
/bin/systemctl status --user foo
root@srv0:~# sudo -u alice systemctl status --user foo
Failed to connect to bus: No such file or directory
我也尝试过使用user@UID
语法,也不起作用:
root@srv0:~# systemctl status user@1001:foo
● user@1001:foo.service - User Manager for UID 1001:foo
Loaded: loaded (/lib/systemd/system/[email protected]; static; vendor preset: enabled)
Active: inactive (dead)
答案1
根据这次讨论 https://lists.freedesktop.org/archives/systemd-devel/2014-March/018227.html 像这样的东西会起作用:
env XDG_RUNTIME_DIR=/run/user/1000 sudo --preserve-env=XDG_RUNTIME_DIR -u alice systemctl status --user foo
您只需要以某种方式获取 XDG_RUNTIME_DIR 即可。