问题
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.
but when trying this:
pids=`pgrep -u $user gnome-panel`
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
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"
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
回答1:
The notification service has been changed for ubuntu 14.04
.
Its called now smth like org.freedesktop.Notifications.service
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 ~$ notify-send “Text of message”
Just update your script which is being launched by udev
to use it.
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”'
回答2:
Combining tomy's answer with hongo's answer to another question elegantly solves the issue for me.
function notify-send() {
#Detect the name of the display in use
local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
#Detect the user using such display
local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)
#Detect the id of the user
local uid=$(id -u $user)
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
}
That function can be used as-is in any script running as root
, as a drop-in replacement for the notify-send
command.
回答3:
To send desktop notification from a background script running as root
(replace X_user and X_userid with the user and userid running X respectively):
sudo -u X_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/X_userid/bus notify-send 'Hello world!' 'This is an example notification.'
This was taken from: https://wiki.archlinux.org/index.php/Desktop_notifications
回答4:
I also was looking into this to fix a bug with a corrupt background on restart. I wanted to add a script to /etc/pm/sleep.d/ to reset it but needed DBUS_SESSION_BUS_ADDRESS to set it (gnome desktop). I found that the DBUS_SESSION_BUS_ADDRESS stored in the /home/user/.dbus/session-bus/* files doesn't match the one in the users environment. But I did find that one available in one of the dbus-daemon commands.
So I was able to get DBUS_SESSION_BUS_ADDRESS via the snipped below and I was successfully able to use it. Replace "[username]" with your user. Hope this helps.
$ export DBUS_SESSION_BUS_ADDRESS=$(pgrep -a dbus-daemon -U [username] | grep -o 'unix:abstract.*$')
回答5:
I struggled with notify-send myself lateley and got it working (under Ubuntu 16.04).
I think there are 2 aspects. The calling user has to have access to the display, where the message shall be displayed and the display has to be specified.
root@mymachine~# who
root tty1 2017-06-19 16:30
<user1> tty7 2017-06-20 07:15 (:0)
<user1> pts/2 2017-06-20 07:42 (:0.0)
root pts/14 2017-06-20 07:05 (<IP1>)
<user1> pts/16 2017-06-20 07:15 (:0.0)
root pts/17 2017-06-19 17:15 (<IP2>)
<user2> 2017-06-20 07:39 (<IP3>:2) #VNC
<user1> pts/26 2017-06-19 18:03 (<IP3>:1.0) #VNC
So what worked for me was
su - <user2> -c "DISPLAY=<IP3>:2 notify-send hi"
I think by switching to the user, you use their ~/.Xauthority. When authorized, specifying the display works.
I used another way to get the authority so far. I looped through the ~/.Xauthority files of every logged-in user and collected their authorizing cookies. Afterwards using the right DISPLAY-Variable works. I even collected the cookie of the display-manager (lightdm in my case)
The ~/.Xauthority-file (after collectiing):
root@mymaching:~# xauth list
<HOSTNAME3>:3 MIT-MAGIC-COOKIE-1 0ebdee59ee015f49066f372b00589420
<HOSTNAME3>:1 MIT-MAGIC-COOKIE-1 74254e326e8904419d005a05a6e95a8c
<HOSTNAME3>/unix:0 MIT-MAGIC-COOKIE-1 54d4cdb35eff20375e486f88e6ac8c74
<HOSTNAME3>:2 MIT-MAGIC-COOKIE-1 c6a07838c7456224799eedfff2be7cd5
My script so far (I guess it is not final yet)
#!/bin/bash
users="$(who | awk '{print $1}' | sort -u | grep -v root)"
rm ~/.Xauthority
touch ~/.Xauthority
for user in $users; do
xauth_file=/home/$user/.Xauthority
while read cookie; do
xauth add $cookie
done <<< "$(xauth -f $xauth_file list | grep $HOSTNAME)"
done
for xauth_file in $(find /var/run/lightdm -type f); do
while read cookie; do
xauth add $cookie
done <<< "$(xauth -f $xauth_file list | grep $HOSTNAME)"
done
for display in $(xauth list | awk '{print $1}' | cut -d/ -f2); do
DISPLAY=$display notify-send "$@"
done
Legend: Items in <> are anonymised. IPX matches HOSTNAMEX
Greetings dasBaschdi
来源:https://stackoverflow.com/questions/28195805/running-notify-send-as-root