Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

Code:

#!/bin/sh
# Backs up the local filesystem on the external HDD

sleep 60

DISPLAY=:0.0

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

*Do backup stuff here*

Error:

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

4 Answers 4

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 ...
share|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 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 Nov 23 '10 at 19:07

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 -

share|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 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 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

share|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.

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.