3

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

5 Answers5

3

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 ...
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • 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 http://superuser.com/questions/190801/linux-wmctrl-cannot-open-display-when-session-initiated-via-sshscreen/190878#190878 and http://unix.stackexchange.com/questions/1596/as-root-can-i-launch-a-graphical-program-on-another-users-desktop/1600#1600 . – Gilles 'SO- stop being evil' Nov 23 '10 at 19:07
  • 1
    This did not work for me, but putting --display=:0.0 as an argument to Zenity worked – hansn Aug 24 '16 at 08:40
1

My situation was a little different, I needed to run a GUI app from cron that had root permissions. The following lines in the shell script fixed this issue for me:

export DISPLAY=:0,0
export XAUTHORITY=/home/{logged in user}/.Xauthority

Of course I'm not really supposed to be doing this as per this article, but I am still yet to find a better workaround.

Patrick
  • 111
  • 2
0

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.

Alex
  • 790
0

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 -

  • 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 –  Nov 29 '10 at 06: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 01 '14 at 07: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 01 '14 at 07:29
0

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