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.
Notify-send not working in python script run with sudo
So after a bit of research, I've found the solution:
import os
def notify(title, message):
userID = subprocess.run(['id', '-u', os.environ['SUDO_USER']],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True).stdout.decode("utf-8").replace('\n', '')
subprocess.run(['sudo', '-u', os.environ['SUDO_USER'], 'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/{}/bus'.format(userID),
'notify-send', '-i', 'utilities-terminal', title, message],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True)
notify-send from within a bash script
I'm pretty new to the linux world, but while looking for a solution for a similar project I found THIS
Tip: An overview on the available icons can be found here. 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.'
Hope this will help others.
Cron with notify-send
I found the answer:
$ crontab -l
# m h dom mon dow command
* * * * * export DISPLAY=:0.0 && export XAUTHORITY=/home/ravi/.Xauthority && sudo -u ravi /usr/bin/notify-send Hey "How are you"
notify-send not working (in script) executed from udev
The main problem is, that the udev-rule will not run in any xorg-related environment per default, thus not knowing which DISPLAY to use. Therefore it will always fail, if You want to echo something into a terminal like a gnome-terminal for example. The script, which shall be executed on the udev-rule-match, must prior to any ui-related execution first export the DISPLAY.
This is done via
export DISPLAY=:0
I assume, that this also will be the problem, and notify-send will just run against the wall.
I am actually also playing with udev-rules, and I managed it to work, though i am acting as root, similar to my answer and this one found already here :
https://unix.stackexchange.com/questions/80882/udev-running-a-shellscript-that-accesses-an-x-display
And also here
Scripts launched from udev do not have DISPLAY access anymore?
You might want also to check zenity. Very helpful for small notifications
Related Topics
When Setting Ifs to Split on Newlines, Why Is It Necessary to Include a Backspace
Unshare --Pid /Bin/Bash - Fork Cannot Allocate Memory
How to Change Rvm Install Location
Walking Page Tables of a Process in Linux
How to Capture All of My Compiler's Output to a File
How to Start Tomcat with Output on Console in Linux
What Is a Way to Read Man Pages in Vim Without Using Temporary Files
Parameter for Shell Scripts That Is Started with Qsub
How to Post Raw Body Data with Curl
Can't Change Tomcat 7 Heap Size
How to Convert Pptx Files to Jpg or Png (For Each Slide) on Linux
Smbclient - Send All Files in Directory
Truncating the First 100Mb of a File in Linux