Running X apps (like zenity) from crontab (solving “cannot open display” problem)
I’ve written a small script that checks whether I have queued mail and that displays a notification icon via “zenity –notification” in case I do. I wanted to run it using ~/.crontab but it wouldn’t run. First, I got error messages from the cron daemon that looked like this:
(zenity:22981): Gtk-WARNING **: cannot open display:
Sure, I hadn’t specified a display. So I tried setting the DISPLAY explicitly in my ~/.crontab:
* * * * export DISPLAY=:0.0 && /path/to/checklog
but then I got:
No protocol specified (zenity:24101): Gtk-WARNING **: cannot open display: :0
The same happened when I tried running other X apps, like audacious. A line in ~/.crontab like this:
* * * * * DISPLAY=:0 /usr/bin/audacious
resulted in errors like this:
No protocol specified /usr/bin/audacious: Unable to open display, exiting.
It turns out I needed to set, in my ~/.bashrc:
xhost local:mpromber > /dev/null
to allow crontab access to my X display. It now works, either using “export DISPLAY=:0.0″ in the ~/.crontab, or alternatively just using
zenity --notification --display=:0.0
in the script that checks for queued mail.
March 18th, 2010 at 10:41
Wow, thanks this was frustrating to get it working.
November 4th, 2010 at 14:00
[...] [...]
November 4th, 2010 at 14:10
Hi, in reply to the latest pingback (comment #2; I’m not signed up to the forum it links to) — what exactly doesn’t work for you? Note that “mpromber” in this line
is my username, so you’d have to substitute your username.
August 18th, 2011 at 19:20
YOU ARE THE BEST ONE
September 1st, 2011 at 16:53
Yeah! Thanks a lot! It really works!
October 20th, 2011 at 20:24
Thanks a lot – I stumbled about this problem, too.
April 2nd, 2012 at 20:37
[...] thanks for speedy reply I was about to try your suggestion but some how i found this link "Running X app from crontab" what i used was the –diplay=:0.0 option in the zenity which is [...]
April 2nd, 2012 at 20:43
[...] you miss understand me about your answer in the other furom. I found my answer on this link " Running X apps on crontab" what i did was i add the –display=:0.0 option in the zenity [...]
April 3rd, 2012 at 04:36
Using ~/.bashrc for the xhost command is not robust. It will only work if one of the Bash Startup Files for an interactive logon shell sources ~/.bashrc. It is common practice to do so but not universal.
If xhost is run unconditionally, it will result in an error when the user logs on at a virtual terminal: xhost: unable to open display “”. Making it conditional on $DISPLAY being set avoids this.
The same code will work for all users by using $LOGNAME instead of a hard-coded user name string.
Putting this all together, put this in ~/.bash_profile:
[[ $DISPLAY ]] && xhost +localhost:$LOGNAME
May 4th, 2012 at 08:56
I’m from Russia and you help so much!!! THANKS!!! =)