Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 4177  / 2 Years ago, sun, july 3, 2022, 2:34:25

I have been working in a script to change audio output when you (dis)connect am HDMI screen/TV. Basically it uses UDEV to check any change in the connection and acts accordingly.



Everything works fine except one thing: I can't use notify-send to warn about the change. I use the following code (output is just a wrapper to subprocess.check_output):



output("sudo -u {0} notify-send "{1}" "{2}"".format(user, title, message))


but I don get any notification at all.



What is really odd is that if I run hdmi_sound_toggle (as a normal user or with sudo) everything works just fine!



So what could be the problem here? There is any better way to present a notification than with notify-send?


More From » 12.04

 Answers
6

The notify-send has trouble putting notifications on a user's screen when called from a script run by root or another user.



You should use:



output("export DISPLAY=:0; sudo -u {0} notify-send "{1}" "{2}"".format(user, title, message))


So, use:



export DISPLAY=:0


Normally a user is on display :0, but to be sure, you can find which display a user is on using who command as follow:



who | grep -m1 ^username.*( | awk '{print $5}' | sed 's/[(|)]//g'


This worked for me in this script.



See also: Can I launch a graphical program on another user's desktop as root?


[#29336] Monday, July 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
itteast

Total Points: 291
Total Questions: 123
Total Answers: 104

Location: Tuvalu
Member since Wed, Mar 29, 2023
1 Year ago
;