我使用以下内容systemd
配置文件zenity
文件每次将具有 SMART 功能的特定设备插入 USB 端口时都会运行图形对话框。这在使用X
显示服务器的 Xubuntu 中运行良好,但zenity
在韦兰服务器。
USER=myName
DISPLAY=:0.0
WAYLAND_DISPLAY=wayland-0
XAUTHORITY=/home/myName/.Xauthority
HOME=/home/myName
SHELL=/bin/bash
我尝试WAYLAND_DISPLAY=wayland-0
添加配置文件在 Ubuntu 23.10 中为 Wayland 服务器创建了文件,但zenity
对话框仍然无法显示。我还需要做什么?
我的设置-01:
/bin/hdd_smart_dump_on_usb_trigger.sh
/etc/00_hdd_smart_dump_on_usb_trigger.conf
/etc/systemd/system/[email protected]
/etc/systemd/system/[email protected]/disable_manual_start.conf
/etc/systemd/system/[email protected]
/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh
/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
内容/etc/00_hdd_smart_dump_on_usb_trigger.conf
:
USER=myName
DISPLAY=:0.0
WAYLAND_DISPLAY=wayland-0
XAUTHORITY=/home/ajthapa/.Xauthority
HOME=/home/myName
SHELL=/bin/bash
内容:/etc/systemd/system/[email protected]
[Unit]
Description=HDD SMART Dump on USB Trigger - service unit for %I
Documentation=man:systemd.unit(5) man:systemd.exec(5) man:systemd.special(7) man:systemd.target(5)
ConditionPathExistsGlob=/sys/class/block/sd*
# RefuseManualStart=true
# RefuseManualStop=true
[Service]
EnvironmentFile=/etc/00_hdd_smart_dump_on_usb_trigger.conf
ExecStart=-/bin/bash -c '/bin/hdd_smart_dump_on_usb_trigger.sh -d %i -t "start_of_session"'
内容:/etc/systemd/system/[email protected]/disable_manual_start.conf
[Unit]
## Allow only to be started/stopped by device events; if manual start is needed
## you should instead call the script which this service calls.
RefuseManualStart=true
RefuseManualStop=true
内容:/etc/systemd/system/[email protected]
[Unit]
Description=HDD SMART Dump on USB Trigger - target unit for %I
Documentation=man:systemd.unit(5) man:systemd.target(5) man:systemd.special(7)
StopWhenUnneeded=yes
Requires=hdd_smart_dump_on_usb_trigger@%i.service
Before=hdd_smart_dump_on_usb_trigger@%i.service
BindsTo=%i.device
RefuseManualStart=true
RefuseManualStop=true
内容/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
:
SUBSYSTEM!="block", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
KERNEL!="sd*", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ENV{DEVTYPE}!="disk", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
## Rules to match specific devices; start:
ATTRS{idProduct}=="2312", ATTRS{idVendor}=="0bc2", GOTO="label__enlisted_devices"
ATTRS{idProduct}=="5590", ATTRS{idVendor}=="0781", GOTO="label__enlisted_devices"
ATTRS{idProduct}!="2312|5590", ATTRS{idVendor}!="0bc2|0781", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
## Rules to match specific devices; end.
LABEL="label__enlisted_devices"
ACTION=="remove", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_for_action_remove"
ACTION!="add", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", ATTRS{idProduct}=="2312", ATTRS{idVendor}=="0bc2", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hdd_smart_dump_on_usb_trigger@dev-%k.target", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==01== UDEV_ACTION==ADD #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | DEVPATH=$env{DEVPATH} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} | ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=%k | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | removable=$attr{removable} | bMaxPower=$attr{bMaxPower} | ]]", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", ATTRS{idProduct}=="5590", ATTRS{idVendor}=="0781", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hdd_smart_dump_on_usb_trigger@dev-%k.target", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==01== UDEV_ACTION==ADD #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | DEVPATH=$env{DEVPATH} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} | ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=%k | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | removable=$attr{removable} | bMaxPower=$attr{bMaxPower} | ]]", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
LABEL="label__hdd_smart_dump_on_usb_trigger_rules_for_action_remove"
ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==02== UDEV_ACTION==REMOVE #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=$kernel | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | model=$attr{model} | removable=$attr{removable} ]]"
LABEL="label__hdd_smart_dump_on_usb_trigger_rules_end"
包含/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
匹配特定设备集的规则,它会在这些设备上执行这些规则/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh
,进而将一些属性写入/var/run/
目录中的文件中。规则文件还会触发 systemd 单元文件,然后执行/bin/hdd_smart_dump_on_usb_trigger.sh
。此脚本使用 zenity 允许用户将 的输出保存smartmontools
到选定位置。
更新:
设置-02:
文件列表:
-rw-r--r-- 1 root root 3018 Feb 19 12:25 /etc/00_hdd_smart_dump_on_usb_trigger.conf
-rw-r--r-- 1 root root 2013 Feb 19 12:15 /etc/systemd/system/[email protected]
-rw-r--r-- 1 root root 196 Aug 8 2020 /etc/systemd/system/[email protected]/disable_manual_start.conf
-rw-r--r-- 1 root root 573 Feb 19 07:42 /etc/systemd/system/[email protected]
-rwxr-xr-x 1 root root 7562 Sep 5 2020 /etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh
-rw-r--r-- 1 root root 7710 Feb 19 13:29 /etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
-rwxr-xr-x 1 root root 5032 Feb 19 13:18 /opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh
-rwxr-xr-x 1 root root 34594 Feb 19 11:58 /opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_on_usb_trigger.sh
-rw-r--r-- 1 myName myName 502 Feb 19 13:30 /home/myName/.config/systemd/user/[email protected]
使用此设置,udev 会写入属性,然后由我的 systemd 系统单元读取,但我的 systemd 用户单元无法启动并在 zenity 对话框中显示转储。我可以使用相同的命令来启动带有 SMART 转储的 zenity。因此,问题可能出在我的 udev .rules 文件和/或我的 systemd 用户单元文件和/或它的命名中。/home/myName/.config/systemd/user/[email protected]
内容:/home/myName/.config/systemd/user/[email protected]
[Unit]
Description=HDD SMART Dump Display Using Zenity Dialogue
Documentation=man:systemd.unit(5) man:systemd.exec(5) man:systemd.special(7) man:systemd.target(5)
Requires=hdd_smart_dump_on_usb_trigger@%i.service
After=hdd_smart_dump_on_usb_trigger@%i.service
ConditionPathExistsGlob=/sys/class/block/sd*
# RefuseManualStart=true
# RefuseManualStop=true
[Service]
Type=oneshot
ExecStart=-/bin/bash -c '/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh --display-dialogue'
内容/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
:
SUBSYSTEM!="block", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
KERNEL!="sd*", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ENV{DEVTYPE}!="disk", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ATTRS{idProduct}=="2312", ATTRS{idVendor}=="0bc2", GOTO="label__enlisted_devices"
ATTRS{idProduct}=="5590", ATTRS{idVendor}=="0781", GOTO="label__enlisted_devices"
ATTRS{idProduct}!="2312|5590", ATTRS{idVendor}!="0bc2|0781", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
LABEL="label__enlisted_devices"
ACTION=="remove", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_for_action_remove"
ACTION!="add", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", ATTRS{idProduct}=="2312", ATTRS{idVendor}=="0bc2", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hdd_smart_dump_on_usb_trigger@dev-%k.target", ENV{SYSTEMD_USER_WANTS}+="hdd_smart_dump_display_using_zenity@dev-%k.system", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==01== UDEV_ACTION==ADD #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | DEVPATH=$env{DEVPATH} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} | ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=%k | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | removable=$attr{removable} | bMaxPower=$attr{bMaxPower} | ]]", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", ATTRS{idProduct}=="5590", ATTRS{idVendor}=="0781", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hdd_smart_dump_on_usb_trigger@dev-%k.target", ENV{SYSTEMD_USER_WANTS}+="hdd_smart_dump_display_using_zenity@dev-%k.system", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==01== UDEV_ACTION==ADD #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | DEVPATH=$env{DEVPATH} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} | ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=%k | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | removable=$attr{removable} | bMaxPower=$attr{bMaxPower} | ]]", GOTO="label__hdd_smart_dump_on_usb_trigger_rules_end"
LABEL="label__hdd_smart_dump_on_usb_trigger_rules_for_action_remove"
ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", RUN+="/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh ==02== UDEV_ACTION==REMOVE #### ENVIRONMENT::::[[ DEVNAME=$env{DEVNAME} | ID_MODEL=$env{ID_MODEL} | ID_SERIAL=$env{ID_SERIAL} | ID_SERIAL_SHORT=$env{ID_SERIAL_SHORT} | ID_VENDOR=$env{ID_VENDOR} | TAGS=$env{TAGS} | ID_ATA=$env{ID_ATA} | ID_ATA_FEATURE_SET_SMART=$env{ID_ATA_FEATURE_SET_SMART} | ID_ATA_FEATURE_SET_SMART_ENABLED=$env{ID_ATA_FEATURE_SET_SMART_ENABLED} | ID_ATA_SATA=$env{ID_ATA_SATA} | ID_ATA_WRITE_CACHE=$env{ID_ATA_WRITE_CACHE} | ID_ATA_WRITE_CACHE_ENABLED=$env{ID_ATA_WRITE_CACHE_ENABLED} | ID_VENDOR_ID=$env{ID_VENDOR_ID} | ID_MODEL_ID=$env{ID_MODEL_ID} ]] #### ATTRIBUTE::::[[ size=$attr{size} | KERNEL=$kernel | idProduct=$attr{idProduct} | idVendor=$attr{idVendor} | manufacturer=$attr{manufacturer} | product=$attr{product} | model=$attr{model} | removable=$attr{removable} ]]"
LABEL="label__hdd_smart_dump_on_usb_trigger_rules_end"
sudo systemctl --no-pager -l status 'hdd_smart_dump_on_usb_trigger*'
插入 USB 设备后的输出:
● [email protected] - HDD SMART Dump on USB Trigger - target unit for dev/sdb
Loaded: loaded (/etc/systemd/system/[email protected]; static)
Active: active since Mon 2024-02-19 14:17:50 +0545; 2min 24s ago
Docs: man:systemd.unit(5)
man:systemd.target(5)
man:systemd.special(7)
Feb 19 14:17:50 ajthapa-vcomputer systemd[1]: Reached target [email protected] - HDD SMART Dump on USB Trigger - target unit for dev/sdb.
没有 的输出systemctl --user --no-pager -l status 'hdd_*'
。
设置-03:
文件:
/etc/00_hdd_smart_dump_on_usb_trigger.conf
/etc/systemd/system/[email protected]
/etc/systemd/system/[email protected]/disable_manual_start.conf
/etc/systemd/system/[email protected]
/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh
/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh
/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_on_usb_trigger.sh
/home/ajthapa/.config/systemd/user/hdd_smart_dump_display_using_zenity.service
内容/home/ajthapa/.config/systemd/user/hdd_smart_dump_display_using_zenity.service
:
[Unit]
Description=HDD SMART Dump Display Using Zenity Dialogue
Documentation=man:systemd.unit(5) man:systemd.exec(5) man:systemd.special(7) man:systemd.target(5)
StopWhenUnneeded=yes
After=hdd_smart_dump_on_usb_trigger@%i.service
#BindsTo=%i.device
RefuseManualStart=true
RefuseManualStop=true
[Service]
Type=oneshot
ExecStart=-/bin/bash -c '/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh --display-dialogue'
[Install]
WantedBy=hdd_smart_dump_on_usb_trigger@%i.service
命令的输出systemctl --user enable hdd_smart_dump_display_using_zenity.service
:
Created symlink /home/ajthapa/.config/systemd/user/[email protected]/hdd_smart_dump_display_using_zenity.service → /home/ajthapa/.config/systemd/user/hdd_smart_dump_display_using_zenity.service.
Unit /home/ajthapa/.config/systemd/user/hdd_smart_dump_display_using_zenity.service is added as a dependency to a non-existent unit [email protected].
'...依赖项添加到不存在的单元...'阻止在用户单元中启动编码脚本。
在 setup-03 中,udev .rules 文件仅触发 systemd 文件的系统单元。
设置-04:
文件列表:
/etc/00_hdd_smart_dump_on_usb_trigger.conf
/etc/systemd/system/[email protected]
/etc/systemd/system/[email protected]/disable_manual_start.conf
/etc/systemd/system/[email protected]
/etc/udev/00_hdd_smart_dump_on_usb_trigger.udev.sh
/etc/udev/rules.d/99-hdd-smart-dump-on-usb-trigger.udev.rules
/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh
/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_on_usb_trigger.sh
/home/ajthapa/.config/systemd/user/[email protected]
/home/ajthapa/.config/systemd/user/[email protected]
内容:/home/ajthapa/.config/systemd/user/[email protected]
[Unit]
Description=HDD SMART Dump Display Using Zenity Dialogue
Documentation=man:systemd.unit(5) man:systemd.exec(5) man:systemd.special(7) man:systemd.target(5)
ConditionPathExistsGlob=/sys/class/block/sd*
RefuseManualStart=true
RefuseManualStop=true
[Service]
Type=oneshot
ExecStart=-/bin/bash -c '/opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh --display-dialogue'
内容:/home/ajthapa/.config/systemd/user/[email protected]
[Unit]
Description=HDD SMART Dump Display Using Zenity Dialogue - target unit for %I
Documentation=man:systemd.unit(5) man:systemd.exec(5) man:systemd.special(7) man:systemd.target(5)
StopWhenUnneeded=yes
Requires=hdd_smart_dump_display_using_zenity@%i.service
Before=hdd_smart_dump_display_using_zenity@%i.service
RefuseManualStart=true
RefuseManualStop=true
系统单元 .target 和用户单元 .target 由 udev 规则触发。通过此 systemd 系统、用户单元和 udev 规则配置,这些用户单元似乎在系统单元之前执行。例如,当 USB 设备首次连接时,zenity 显示空白。当设备弹出并再次连接时,用户单元首先执行并读取由系统单元运行的脚本写入的先前值,因此显示过时值(先前插入的设备的值)。
更新:在 setup-04 中,在用户单元 .service 文件中,我添加了大约 3-5 秒的延迟,并且设置工作按预期进行,但这不是我想要的,我希望通过单元文件中的 Systemd 设置来完成此操作。
ExecStart=-/bin/bash -c 'sleep 3s; /opt/hdd_smart_dump_on_usb_trigger/bin/hdd_smart_dump_display_with_zenity.sh --display-dialogue'
我在.service 用户单元文件中使用。
知道我在这些设置中做错了什么吗?
答案1
这是在 Systemd 用户服务中触发对话框的演示zenity
,该对话框由插入(添加)USB 磁盘触发的 UDEV 规则触发...这个答案的目的是作为模板或样板,它并不意味着作为您的用例的实际解决方案。
创建用户服务单元文件:
cat <<'EOF'> ~/.config/systemd/user/my-test-zenity.service
[Unit]
Description=Test zenity dialog from a user sevice
After=graphical-session.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'zenity --info --text="USB Disk Added"'
[Install]
WantedBy=graphical-session.target
EOF
将此情况告知 Systemd 守护进程:
systemctl --user daemon-reload
启用它并启动它:
systemctl --user enable --now my-test-zenity.service
...这应该已经触发了zenity
对话窗口并且你应该已经看到它了。
创建 UDEV 规则:
cat <<'EOF' | sudo tee /etc/udev/rules.d/70-user-service.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ENV{DEVTYPE}=="disk", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}+="my-test-zenity.service"
EOF
... 注意SYSTEMD_USER_WANTS
用户服务与SYSTEMD_WANTS
系统服务。
重新启动 UDEV 服务以捕获新的规则文件:
sudo systemctl restart udev
测试一下:
插入 USB 磁盘,您应该会看到zenity
对话框窗口被触发...只要图形用户会话正在运行,这应该在任何显示服务器上同样触发...更多相关讨论这里。
笔记:
以上评论摘录:...用户单元不能引用或依赖系统单元,反之亦然...它们在完全隔离的进程中运行,由两个不同的 Systemd 管理器(系统单元管理器和用户单元管理器)管理...