Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Have you tried sticking a notify-send in the script that just says "hi" or something simple? Just to see if it can be called properly from the script. Perhaps the formatting is off. –  Steelsouls Sep 21 '13 at 16:56
add comment

2 Answers

up vote 3 down vote accepted

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?

share|improve this answer
add comment

You can use pynotify instead of a shell command. It's quite simple and is installed by default in Ubuntu. A quick sample:

import pynotify

pynotify.init("Name")
n = pynotify.Notification("Name", "This is a notification")
n.show()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.