我正在使用带有 KDE/Awesome WM 的 Arch Linux。我正在尝试
notify-send
与cron
.
我试过设置DISPLAY
/XAUTHORITY
变量,并notify-send
使用“sudo -u”运行,但都没有结果。
我能够从会话中以交互方式调用通知发送并获取通知。
FWIW,cron 作业运行良好,我通过将内容回显到临时文件进行了验证。只是“通知发送”不起作用。
代码:
[matrix@morpheus ~]$ crontab -l
* * * * * /home/matrix/scripts/notify.sh
[matrix@morpheus ~]$ cat /home/matrix/scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
echo "testing cron" >/tmp/crontest
sudo -u matrix /usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest
[matrix@morpheus ~]$ cat /tmp/crontest
testing cron
now tested notify-send
[matrix@morpheus ~]$
Run Code Online (Sandbox Code Playgroud)
如您所见,通知发送之前和之后的回声有效。
我也试过设置DISPLAY=:0.0
更新:我搜索了一些,发现需要设置DBUS_SESSION_BUS_ADDRESS。在使用我从交互式会话中获得的值对其进行硬编码后,屏幕上每分钟都会弹出一个小小的“你好”消息!
但问题是这个变量不是永久的,所以我会尝试那里建议的命名管道解决方案。
[matrix@morpheus ~]$ cat scripts/notify.sh
#!/bin/bash …
Run Code Online (Sandbox Code Playgroud) 除了 ksh。是否有可能在没有任何痛苦的情况下做到这一点(正则表达式)?