这个问题不重复启动 - GUI 准备好后 Systemd 运行脚本? - Unix 和 Linux 堆栈交换因为这涉及到正确目标的明智选择,而 Url 只涉及最终结果。该问题旨在强调目标选择中的决策。
过去运行 GUI 应用程序的经验
我成功地运行了这些 GUI 服务,而无需在 Ubuntu 20.04 中定义环境变量。我在 Linux Mint 20.3 中遇到问题。我认为原因是没有合适的目标。例如,以下命令在 Ubuntu 中成功运行
[Unit]
Description=Browser Service
[Service]
ExecStart=firefox
Type=simple
Restart=on-failure
RestartSec=10
SyslogIdentifier=BrowserService
# Refer: [Adjust or silence a systemd service’s logging levels](https://www.ctrl.blog/entry/systemd-log-levels.html)
StandardOutput=null
StandardError=null
[Install]
WantedBy=graphical-session.target # Ubuntu
系统信息:内核:5.4.0-94-generic x86_64 位:64 编译器:gcc v:9.3.0 桌面:Cinnamon 5.2.7 发行版:Linux Mint 20.3 Una 基础:Ubuntu 20.04 焦点
因为,用户单位不能引用或依赖于系统单位或其他用户的单位。我列出了所有用户目标单元文件
$ find /usr/lib/systemd/user ~/.local/share/systemd/user /etc/systemd/user ~/.config/systemd/user -type f -name '*.target' | sort
find: ‘/home/nikhil/.local/share/systemd/user’: No such file or directory
/home/nikhil/.config/systemd/user/default.target
/usr/lib/systemd/user/basic.target
/usr/lib/systemd/user/bluetooth.target
/usr/lib/systemd/user/default.target
/usr/lib/systemd/user/exit.target
/usr/lib/systemd/user/gnome-session-failed.target
/usr/lib/systemd/user/gnome-session-initialized.target
/usr/lib/systemd/user/gnome-session-manager.target
/usr/lib/systemd/user/gnome-session-pre.target
/usr/lib/systemd/user/gnome-session-shutdown.target
/usr/lib/systemd/user/gnome-session.target
/usr/lib/systemd/user/[email protected]
/usr/lib/systemd/user/gnome-session-wayland.target
/usr/lib/systemd/user/[email protected]
/usr/lib/systemd/user/gnome-session-x11-services.target
/usr/lib/systemd/user/gnome-session-x11.target
/usr/lib/systemd/user/[email protected]
/usr/lib/systemd/user/graphical-session-pre.target
/usr/lib/systemd/user/graphical-session.target
/usr/lib/systemd/user/gsd-a11y-settings.target
/usr/lib/systemd/user/gsd-color.target
/usr/lib/systemd/user/gsd-datetime.target
/usr/lib/systemd/user/gsd-housekeeping.target
/usr/lib/systemd/user/gsd-keyboard.target
/usr/lib/systemd/user/gsd-media-keys.target
/usr/lib/systemd/user/gsd-power.target
/usr/lib/systemd/user/gsd-print-notifications.target
/usr/lib/systemd/user/gsd-rfkill.target
/usr/lib/systemd/user/gsd-screensaver-proxy.target
/usr/lib/systemd/user/gsd-sharing.target
/usr/lib/systemd/user/gsd-smartcard.target
/usr/lib/systemd/user/gsd-sound.target
/usr/lib/systemd/user/gsd-usb-protection.target
/usr/lib/systemd/user/gsd-wacom.target
/usr/lib/systemd/user/gsd-wwan.target
/usr/lib/systemd/user/gsd-xsettings.target
/usr/lib/systemd/user/paths.target
/usr/lib/systemd/user/printer.target
/usr/lib/systemd/user/shutdown.target
/usr/lib/systemd/user/smartcard.target
/usr/lib/systemd/user/sockets.target
/usr/lib/systemd/user/sound.target
/usr/lib/systemd/user/timers.target
- 活动单元文件如下:
$ systemctl --user list-units --type target --state active
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
default.target loaded active active Main User Target
paths.target loaded active active Paths
sockets.target loaded active active Sockets
timers.target loaded active active Timers
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
5 loaded units listed.
任务
我想使用 Systemd 用户服务单元启动 Firefox。我假设我可以使用目标/usr/lib/systemd/user/graphical-session.target
。
cat /usr/lib/systemd/user/graphical-session.target
[Unit]
Description=Current graphical user session
Documentation=man:systemd.special(7)
Requires=basic.target
RefuseManualStart=yes
StopWhenUnneeded=yes
但是,我不确定这是否是正确的目标。
$ cat /home/nikhil/.config/systemd/user/Browser.service
[Unit]
Description=Browser Service
[Service]
ExecStart=firefox
Type=simple
Restart=on-failure
RestartSec=10
SyslogIdentifier=BrowserService
# Refer: [Adjust or silence a systemd service’s logging levels](https://www.ctrl.blog/entry/systemd-log-levels.html)
StandardOutput=null
StandardError=null
[Install]
WantedBy=graphical-session.target
我启用了服务文件并重新加载了systemd用户恶魔
$ ls -lA /home/nikhil/.config/systemd/user/graphical-session.target.wants/
total 0
lrwxrwxrwx 1 nikhil 49 Jan 14 13:30 Browser.service -> /home/nikhil/.config/systemd/user/Browser.service
$ systemctl --user daemon-reload
问题
- 但是注销并重新登录后,firefox 无法启动。有什么建议么?
- 我选择了正确的目标吗?如果不是,我应该选择什么?