Skip to navigation

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.

12 Responses to “Running X apps (like zenity) from crontab (solving “cannot open display” problem)”

  1. man Says:

    Wow, thanks this was frustrating to get it working.

  2. Hello, Zenity + crontab Says:

    […] […]

  3. Marianne Says:

    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

    xhost local:mpromber > /dev/null

    is my username, so you’d have to substitute your username.

  4. serpro Says:

    YOU ARE THE BEST ONE

  5. Pavel Says:

    Yeah! Thanks a lot! It really works!

  6. Valentin Says:

    Thanks a lot – I stumbled about this problem, too.

  7. how to accomplished to run zenity in cronjob Says:

    […] 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 […]

  8. How to accomplished to run zenity in cronjob? - The UNIX and Linux Forums Says:

    […] 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 […]

  9. Charles Says:

    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

  10. Christya Says:

    I’m from Russia and you help so much!!! THANKS!!! =)

  11. Sagar Says:

    Hey, thanks so much. This was frustrating me no end :)

  12. André Luis Castillo Says:

    I’m from Brazil and you help so much!!! THANKS!!! =)