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

3 Answers 3

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

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

I was researching a consistent way to notify all X users (as root) and my research (looking at a bunch of places on the net) indicated that although it was possible to use notify-send in this way, it required scripting to find DBUS session info and/or DISPLAY's in use and/or Xauthority file names and locations, and even then sometimes people still couldn't get it to work. In summary, trying to bend notify-send to your will to enable broadcasting to X users will prove to be unreliable (particularly if you upgrade or change distro at some point in the future).

However... on my system the wall command worked like a charm and is incredibly simple to use. The only downside is that it also broadcasts the message to all text terminals/consoles as well. If you don't mind that, then wall might be a better and more reliable option. Note: the user invoking wall must be root.

share|improve this answer

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.