PDA

View Full Version : [SOLVED] zenity: Gtk-WARNING **: cannot open display



Jeff294
April 19th, 2008, 07:15 AM
Howdy!
I'm using a script that is called by UDEV whenever I mount my USB thumb drive, which automatically makes a backup of the thumb drive. It works fine. I thought I'd add a question dialog using zenity, to ask the user (me) whether to skip the backup this time. My zenity question dialog works just fine when run from a test script on my desktop, but when added to the UDEV script (which I think runs as root) the zenity call gets this error message:
(zenity:8596): Gtk-WARNING **: cannot open display:

From reading posts here and thru Google, I think the script running as root doesn't have access to my xterm or GUI display, but here my knowledge runs out and I don't know how to fix that. I'm guessing maybe setting an environment variable at the beginning of my script, but I don't really know what variable or value.

Anybody know how to fix this? (I'm using Hardy beta, with up-to-date updates).

Thanks so much!
Ubuntu rocks!
...Jeff

x1a4
April 19th, 2008, 04:42 PM
Try running the script using sudo (or gksudo) with the -s option. For example:

sudo -s -u username script

I'm not 100% certain if this will work, but I've always been able to run X applications whenever I use sudo -s.

In order to avoid having to type a password, using /usr/sbin/visudo, add this to your sudoers file:

username ALL=(ALL) NOPASSWD:/path/to/script

Jeff294
April 19th, 2008, 06:38 PM
Actually, I'm not running the script, it is being automatically run by the UDEV utility, which can be set up to run user supplied scripts when certain events take place, like when a thumb drive is mounted. It always runs its scripts as root, and I don't think I can change that. I think what's happening is that I've coded the script to throw up a message and wait for a reply (using zenity), but since the script is running as root, it doesn't know how to access my gui desktop, and so the message/reply fails. I'm thinking there must be something I need to add to the script so it knows how to access my gui display when it needs to.

x1a4
April 20th, 2008, 12:34 AM
Inside the script try using sudo (or gksudo) to run zenity. See if this works:

sudo -u $USER zenity

Jeff294
April 20th, 2008, 01:06 AM
That seemed like a really good idea, but unfortunately it didn't work. I tried both sudo and gksudo, and got the same error message. Thanks for the suggestion though!

happyhamster
April 20th, 2008, 01:21 AM
Take a look at:
http://bbs.archlinux.org/viewtopic.php?pid=291934

x1a4
April 20th, 2008, 02:03 AM
Try running
xhost local:user before gksudo. Where user is the name of the user you want to run as.

Jeff294
April 20th, 2008, 02:44 AM
Thanks for that suggestion as well: I read the thread you sent me the link for, and I tried adding this to the script:

export DISPLAY=:0.0

It still doesn't work (zenity, that is), but now I get a new error message:

No protocol specified

in addition to the Gtk warning message I was getting before. Is that significant of something?

Thank you for your time trying to help!

Jeff294
April 20th, 2008, 02:55 AM
I tried the xhost local:user -- still getting the same errors:

No protocol specified

(zenity:7522): Gtk-WARNING **: cannot open display: :0.0

Jeff294
April 20th, 2008, 06:14 AM
Yippee! With a little repeated testing I finally got it to work, using a combination of the clues you good people have given me.
Here's what I eventually coded in the script (which is executed by UDEV):



su $USER -c 'xhost local:$USER; zenity --question --text "Continue or cancel?" '


I couldn't tell you WHY it works, but it does! Now when I plug in the USB thumb drive, the zenity question box appears and waits for an answer.

Thanks to all who responded!
Ubuntu Forums Rock!

tthorb
September 25th, 2009, 08:57 AM
I had problems with sending messages with Crontab to a user, since there are 20 LTSP-thin clients (Edubuntu) here. But - the solution was quite easy.

To find the correct display number, you can do this:


ps -ef |grep ^USERNAME|grep screensa| awk '{ print $2 }'| strings /proc/`xargs`/environ |grep DISPLAY

This checks the number of a a process for the correct user (in this case, I picked screensaver)

In the bash script, I've used this like this:


dispp=`ps -ef |grep ^USERNAME|grep screensa| awk '{ print $2 }'`
dispn=`strings /proc/$dispp/environ |grep DISPLAY|sed 's/.*=\(.*\)\..*/\1/' `
DISPLAY="$dispn" su USERNAME -c '/usr/bin/zenity --question --text "Question" '

rosencrantz
September 5th, 2011, 07:05 PM
Thanks for that suggestion as well: I read the thread you sent me the link for, and I tried adding this to the script:

export DISPLAY=:0.0



it works for me with
env DISPLAY=:0.0 zenity --question
running from a root text console.