7

After doing a "sudo su -" on an Ubuntu 12.04 notebook I did a "crontab -e", added this:

* * * * * env DISPLAY=:0.0 /usr/bin/gnome-calculator

and waited for minutes. Nothing Happened. I don't have any external monitors and if I run this command "env DISPLAY=:0.0 /usr/bin/gnome-calculator" in the terminal, it just works. But not from cron. Why?

The syslog only contains this:

May 24 14:37:01 localhost cron[1227]: (root) RELOAD (crontabs/root)
May 24 14:37:01 localhost CRON[16432]: (root) CMD (env DISPLAY=:0.0 /usr/bin/gnome-calculator )

And I already tried an "xhost +localhost".

[root@NOTEBOOK /var/log] xhost
access control enabled, only authorized clients can connect
INET:localhost.localdomain
SI:localuser:USERNAME
[root@NOTEBOOK /var/log] 

So the solution for another question like this on askubunut didn't worked.

3
  • 1
    Looks like the title is misleading. It's not "How ...", it's "... not working", isn't it?
    – int_ua
    May 24, 2014 at 13:20
  • 2
    It's probably not a good idea to put this in the root crontab un the first place. Have you tried putting it un your user crontab?
    – fkraiem
    May 24, 2014 at 13:52
  • @fkraiem: please post this as answer because this was the problem. If I put the cronjob in the users cron IT WORKS!! (the user with I am logged in ) May 25, 2014 at 10:01

5 Answers 5

5

Rewritten as an answer per OP's request.

It's probably not a good idea to put this in the root crontab un the first place. Have you tried putting it un your user crontab?

2
  • 1
    This would be because access to connect to X servers is usually controlled these days via either the user id running the process or xauth cookies, stored in either $XAUTHORITY or $HOME/.xauthority. When you login to X, the userid you logged in as is set up with permission to connect, but other accounts, such as root, are not.
    – alanc
    May 31, 2014 at 19:18
  • @alanc So can root give itself authority to use the display? or can a user give permission to root? Sep 20, 2018 at 0:25
5

I think this is the issue:

* * * * * env DISPLAY=:0.0 /usr/bin/gnome-calculator

Setting to it to this seems to work:

* * * * * env DISPLAY=:0 /usr/bin/gnome-calculator

The env DISPLAY=:0 tells it to to export to the display :0 - you can find yours under TTY in the output of w

Then run crontab -e as your normal user where you want the application to run, and paste in this line:

* * * * * env DISPLAY=:0 /usr/bin/gnome-calculator

If you don't want to to crontab -e to edit it using vim, run this beforehand to use nano instead:

export EDITOR=nano ; export VISUAL=nano

Note the asterisks means it will run every minute, every hour and every day, so it will probably get VERY annoying unless you remove it.

If it still does not work, try running this command:

sudo bash -c "echo $(whoami) > /etc/cron.allow"

And, if you don't want to muck around with text files, you can use gnome-schedule instead.

References:

2
  • Either :0 or :0.0 should work fine - the second just explictly specifies screen 0 of the display, where the first implies it (since screen 0 is the default if none is specified).
    – alanc
    May 31, 2014 at 19:16
  • @alanc - I tried both and only :0 worked
    – Wilf
    May 31, 2014 at 19:33
3

Cron running under root does not have access to display if the current x user is not root.

We just need to add root user to x, you can do it at login time with a startup script

xhost local:root

For test purpose we can just run than command under current user and relaunch the root cron job

2

In order to work on Ubuntu 18 you have to:

-find your tty as a user in with w (e.g. ":1")

-then * * * * * env DISPLAY=:1 /usr/bin/gnome-calculator

0

Since this site requires 50 reputation to add comments, I will add an answer in response to @intika's answer.

If you want to run the command from a root cron table, you could run the command as the primary non-root user with:

* * * * * env DISPLAY=:0 sudo -i su USERNAME -c "/usr/bin/gnome-calculator"

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.