通过udev规则脚本连接USB时如何弹出通知

时间:2020-03-03 11:01:46

标签: linux notifications udev su notify-send

连接USB时,我尝试弹出一个通知。 我写了一个udev规则,该规则可以正常工作并运行我的shell脚本以弹出通知 ACTION=="add", ATTRS{bInterfaceClass}=="08", RUN+=PATH

我认为该脚本的运行时间有限,因此我在每个命令中都使用了“&”。 另一件事是,这种类型的脚本在没有上下文的情况下运行,因此我提供了export DISPLAY=:0.0和Xauthority 我还获得了 DBUS_SESSION 路径。 问题是重新启动系统以完成任务后,我必须与 non-root 用户手动运行脚本,然后在USB连接时将弹出该脚本。 我认为发生这种情况是因为udev以root权限运行它, 我已经尝试使用sudo -u和su -c调用notify-send命令。

我的脚本:

#!/bin/sh
{
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
set -x
xhost local:root
DISPLAY=:0.0
export DISPLAY
export XAUTHORITY=/var/lib/lightdm/.Xauthority
notify-send -t 10000 -u normal 'USB Detected' 'starting to create snapshot\nPlease do not remove the device'
echo $DISPLAY > debug
python /home/magshimim/Desktop/test.py
} &

0 个答案:

没有答案