2

I put this line in the crontab:

1 * * * * DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl

The script is executable.

If paste that command in the terminal--that is, the string without stars, it works perfectly:

DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl

I tried:

sudo service cron restart

But the problem persists. How can I get cron to run that command hourly?


Update. Got it working with ROOT crontab:

1 * * * * DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 XAUTHORITY=/home/user/.Xauthority /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
ngc3o34
  • 65
  • 1
    This makes no sense – Panther Jul 26 '14 at 20:32
  • The approach proposed by the OP is not a good idea because within this approach the ownership of some files and directories inside /run/user/<uid>/dconf/ will be changed, thus some errors will be generated. Please read this answer. – pa4080 Sep 14 '17 at 12:18

1 Answers1

5

I guess my first guess about the way the DISPLAY variable is handled is wrong. I think the problem somehow related to cron using a non-interactive shell. A test script using an infinite read loop failed to run. However, when I tested by opening a new tab:

* * * * * DISPLAY=:0 /usr/bin/gnome-terminal --tab -e /home/murukesh/test.sh

it worked fine.


According to this SU question and this Ubuntu Forums post, you may have to either export the $DISPLAY variable or use env:

1 * * * * env DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl

(or)

1 * * * * export DISPLAY=:0.0 && /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl

This maybe due to the shell used by cron being sh and not bash (see What's the difference between set, export and env and when should I use each?). Also have a look at Script doesn't run via crontab but works fine standalone.

muru
  • 197,895
  • 55
  • 485
  • 740
  • Thanks for answer! But both options didn't work. ls -l /usr/bin/gnome-terminal gives -rwxr-xr-x 1 root root 301520 Dec 16 2013 /usr/bin/gnome-terminal. So I used root crontable: * * * * * env DISPLAY=:0.0 XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl 2>/home/user/Desktop/crondbg. In crondbg: ** (gnome-terminal:20533): WARNING **: Could not open X display. No protocol specified. Failed to parse arguments: Cannot open display:. Any idea? – ngc3o34 Jul 27 '14 at 12:42
  • @ngc3o34 please don't accept answers until your problem has been solved. That said, programs in /usr/bin are usually always owned by root, so that doesn't mean anything. Using root's crontab because gnome-terminal is owned by root won't help. What error do you get when you use your own crontab? Also, is you DISPLAY actually :0.0? Check with echo $DISPLAY in a normal terminal. – muru Jul 27 '14 at 12:46
  • nothing. empty crondbg – ngc3o34 Jul 27 '14 at 13:00
  • echo $DISPLAY gives ":0" but DISPLAY=:0.0 also works in terminal – ngc3o34 Jul 27 '14 at 13:02
  • new method works in terminal. cron still resist to participate. only result is crondbg updated, but nothing inside. may be I should set another variable like PATH? just guessing... – ngc3o34 Jul 27 '14 at 15:05
  • @ngc3o34 probably. If the script isnt private, can you post it here? Use http://paste.ubuntu.com and add a link in the question. There might be some other assumption. – muru Jul 27 '14 at 15:07
  • Alternatively you could try to wrap the command in bash -c '...'. – Sparhawk Jul 27 '14 at 15:16
  • @Sparhawk was one of the first things that I tested. Didn't help. – muru Jul 27 '14 at 15:17
  • I think script has nothing to do with that. Because this: DISPLAY=:0 vlc works, and this: DISPLAY=:0 /usr/bin/gnome-terminal doesn't – ngc3o34 Jul 27 '14 at 15:43
  • @muru 2>/home/user/Desktop/crondbg at the end – ngc3o34 Jul 27 '14 at 17:01
  • @ngc3o34 and * * * * * DISPLAY=:0 /usr/bin/gnome-terminal is silently failing? – muru Jul 27 '14 at 17:02
  • @muru if using user's crontab - yes – ngc3o34 Jul 27 '14 at 18:17
  • I found that export DISPLAY=:.. is not enough in most cases. Check my answer here. – pa4080 Sep 14 '17 at 12:20