Running notify-send as root Running notify-send as root linux linux

Running notify-send as root


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.


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


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