I'm trying to make a simple weekly Anacron job that backs up my computer if I click yes on the dialog. The script I wrote works fine if run manually, but when Anacron runs it, nothing happens and I see Gtk-WARNING **: cannot open display in the logs. Apparently the script is run at a stage where graphical operations cannot be run. Is there any way to get this dialog to open from an Anacron job?


# Backs up the local filesystem on the external HDD

sleep 60


zenity --question --text "Do you want to backup? Be sure to turn on the HDD."
if [ $? -ne 0 ]
    then exit 1

*Do backup stuff here*


(zenity:2351): Gtk-WARNING **: cannot open display: 
run-parts: /etc/cron.daily/backup-on-external exited with return code 1

It's probably just running without the $DISPLAY environment variable. If you echo $DISPLAY in your shell you can see what its value is (most likely :0.0), and then you can specify that in the crontab file:

DISPLAY=:0.0 dialog ...
| improve this answer | |
  • 3
    And the reason why it's running with no DISPLAY is that (ana)cron jobs are supposed to be able to run even if nobody is logged in. Relying on someone being logged in is not a good idea for something that should be automatic like a backup script (a better design would be to perform the backup if the right media is in place, and rely on the usual failure notification (through e-mail) to alert the user if the right media was not in place and the backup could not be made). – Gilles 'SO- stop being evil' Nov 22 '10 at 19:45
  • I added the DISPLAY=:0.0 line at the beginning but still got the same error. And :0.0 is the correct value for my system. – Cory Walker Nov 23 '10 at 18:30
  • @Cory: You may also need XAUTHORITY as well. The default, used if the variable is unset, is ~/.Xauthority. Some systems (e.g. Ubuntu) use a different file, and it can be a little hard to find out. If that's the issue, see superuser.com/questions/190801/… and unix.stackexchange.com/questions/1596/… . – Gilles 'SO- stop being evil' Nov 23 '10 at 19:07
  • This did not work for me, but putting --display=:0.0 as an argument to Zenity worked – hansn Aug 24 '16 at 8:40

You should use user's crontab instead of system-wide.

Try crontab -e (opens user's crontab in $EDITOR) or echo 'your crontab line here' | crontab -

| improve this answer | |
  • Also, instead of 'if blah-bla-bla', you can use something like: zenity --question --text "Do you want to backup? Be sure to turn on the HDD." || exit 1 – lazyklimm Nov 29 '10 at 6:45
  • This thread is about anacron, not cron ... so I think the anacron way of defining a user-specific setup should be used. – Alex Aug 1 '14 at 7:14
  • Warning : echo 'your crontab line here' | crontab - will replace all your crontab jobs with just this line. Use { crontab -l | grep -v "somekeyword" ; echo "my new crontab line # somekeyword" } | crontab to add/update one line while not causing duplicates. – Stéphane Gourichon Sep 1 '14 at 7:29

works for me with DISPLAY=:0.0 but I have just one user on the system so I specify that user in /etc/crontab

| improve this answer | |

You can define a user-specific anacrontab:

sudo anacron -t ${HOME}/.anacron/anacrontab -S ${HOME}/.anacron/spool

-t = Use specified anacrontab, rather than the default

-S = Use the specified spooldir to store timestamps in, this option is required for users who wish to run anacron themselves.

Add the command in your ~/.profile to execute when you login.

Allow to execute 'sudo anacron' without password by adding the following line to '/etc/sudoers':

user    ALL=(ALL:ALL) NOPASSWD: /usr/sbin/anacron

(Replace 'user' with your username)

Like that the $DISPLAY and the $XAUTHORITY of the user should be set.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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