以root身份运行notify-send

问题描述:

我试图在插入USB设备时收到通知,为此,我使用udev规则来跟踪其插入时刻,然后从那里启动脚本. 脚本的想法是使用链接.

I am trying to get a notification when pluging in a USB device, for this I use a udev rule to track the moment it is pluged and from there I launch a script. The idea on the script was to use what it is explained in the link.

但是尝试此操作时:

pids=`pgrep -u $user gnome-panel`

我发现gnome面板不存在.用谷歌搜索这个工作范围,我发现很少有人抱怨这个工作范围不再起作用.因此,我对该主题进行了一些研究,并提出了这个(notify-plugin2.sh):

I found that gnome-panel is not there. Googled this work arround and I found quite few people complaining that this work arround is no longer working. So I did a bit of research on the subject and came up with this (notify-plugin2.sh):

#!/bin/bash

DBUS_SESSION_BUS_ADDRESS=$(cat /home/user/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0 | grep DBUS_SESSION_BUS_ADDRESS= | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//')

su user Test.sh $DBUS_SESSION_BUS_ADDRESS

以获得DBUS_SESSION_BUS_ADDRESS,然后再将用户切换为非root用户.如果我没记错的话,这种说法行得通,因此根据上面链接中的代码,我制作了另一个脚本(Test.sh)

to get the DBUS_SESSION_BUS_ADDRESS before switching user to a non root user. This statement, if I am not wrong works, so based on the code from the link above I made this other script (Test.sh)

#!/bin/sh
user=`whoami`
title="Test"
timeout=30000
icon="~/Pictures/PicturesForPwrPoint/Pluged.jpg"

DBUS_SESSION_BUS_ADDRESS=$1

echo $DBUS_SESSION_BUS_ADDRESS
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \ notify-send -u low -t $timeout -i "$icon" "$title"

对于我在其他代码上看到的内容,唯一的问题是获取DBUS_SESSION_BUS_ADDRESS,如果我没有记错,我可以拥有它.

For what I can see on the other code, the only problem was getting the DBUS_SESSION_BUS_ADDRESS, and if I am not wrong, with this I can have it.

所以我的问题是,为什么启动时屏幕上没有精美的弹出消息?

So my question is, why there isn't a fancy pop-up message on my screen when launching?

sudo sh notify-plugin2.sh

ubuntu 14.04的通知服务已更改.

它现在的叫法像org.freedesktop.Notifications.service

您可以在此处中查看有关Notification On Screen Display可能性的更多信息.

You can check here for more information about Notification On Screen Display possibilities.

您还可以使用以下命令行发送自己的消息

Also you can use following command line to send your own messages

user @ machine〜$通知发送消息文本"

user@machine ~$ notify-send "Text of message"

只需更新由udev启动的脚本即可使用它.

Just update your script which is being launched by udev to use it.

要变通解决此问题,请注意以root用户身份运行notify-send命令.

To workaround the problem realted to running the notify-send command as root.

尝试以普通用户身份运行,即

Try to run is as your normal user, i.e.

su <YOURUSER> -c 'notify-send "Text of message"'