我正在尝试使用systemd
使用发送桌面通知的服务.sh
脚本与notify-send
命令。我的剧本notif.sh
如下:
#!/bin/bash
notify-send "Hello World"
和我的 systemd 服务notifme.service
是下一个:
[Unit]
Description=should launch a desktop notification
[Service]
User=user
Type=simple
ExecStart=/bin/bash /home/user/notif.sh
Environment="DISPLAY=:0" "XAUTHORITY=/home/user/.Xauthority"
当我启动服务时,除了不显示通知之外,一切似乎都运行良好。当我手动运行脚本时,通知脚本可以正常工作。
当我跑步时systemctl status notifme.service
输出是:
● notifme.service - should launch a desktop notification
Loaded: loaded (/etc/systemd/system/notifme.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2022-02-13 14:39:00 CET; 39s ago
Main PID: 15771 (bash)
Tasks: 9 (limit: 18678)
Memory: 3.5M
CGroup: /system.slice/notifme.service
├─15771 /bin/bash /home/user/notif.sh
├─15773 notify-send Hello World
├─15780 dbus-launch --autolaunch=017e96ffe51b466384d899f21cbecdc5 --binary-syntax --close-stderr
├─15781 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
├─15783 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
└─15784 /usr/bin/plasma_waitforname org.freedesktop.Notifications
feb 13 14:39:00 slimbook systemd[1]: Started should launch a desktop notification.
feb 13 14:39:00 slimbook dbus-daemon[15781]: [session uid=1000 pid=15779] AppArmor D-Bus mediation is enabled
feb 13 14:39:00 slimbook dbus-daemon[15781]: [session uid=1000 pid=15779] Activating service name='org.freedesktop.Notifications' requested by ':1.0' (uid=1000 pid=15773 comm="notify-send Hello World " label="unconfined")
有谁知道为什么不显示通知?提前致谢
问了一下同事,好像是这样的DBUS_SESSION_BUS_ADDRESS
从服务运行脚本时变量不可用。只需添加export
对脚本执行以下命令就足够了:
#!/bin/bash
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"
notify-send "Hello World"
显然,在服务文件中,Environment
没有必要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)