Running notify-send as root

Issue

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

Solution

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”'

Answered By – deimus

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published