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
2
  • 1
    This makes no sense
    – Panther
    Jul 26, 2014 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, 2017 at 12:18

1 Answer 1

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.

13
  • 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, 2014 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, 2014 at 12:46
  • nothing. empty crondbg
    – ngc3o34
    Jul 27, 2014 at 13:00
  • echo $DISPLAY gives ":0" but DISPLAY=:0.0 also works in terminal
    – ngc3o34
    Jul 27, 2014 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, 2014 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .