6

Updates START

  1. A friend of mine just answered this. I need to do this export DISPLAY=:0 && <my GUI dependent command here> Waiting for him to post the answer here.
  2. I have tried /opt/google/chrome/chrome PARTICULAR_GOOGLE_DOC_URL & without the & too. Still fails. Succeeds directly on command line.
  3. There is no /var/log/cron file on my machine. Subsidiary question: How can I get it?

Updates END

I have a Google Doc that I need to update at regular intervals multiple times a day. The content that would form a particular update cannot be predicted or determined programatically. So, the best solution I have is to have the particular Google Doc page open up automatically at fixed intervals and then I manually add the data to it.

Now, the problem is that what works directly on the command line does not work when used in crontab. Below is what I have tried (using 2mins interval just for testing) -

*/2 * * * * date >> /tmp/crontest
*/2 * * * * /opt/google/chrome/chrome PARTICULAR_GOOGLE_DOC_URL &
  1. If I cat /tmp/crontest , I see timestamps added there for every 2nd minute
  2. If I just try /opt/google/chrome/chrome PARTICULAR_GOOGLE_DOC_URL on the shell, it works as expected
  3. But PARTICULAR_GOOGLE_DOC_URL does not get opened in my chrome every 2nd min as it should. (again: I am using 2mins interval just for testing)

What am I doing wrong? How should I fix this? Any alternate way of achieving what I need done?

Thanks in advance.

3
  • 1
    I doubt this wil work, you test on the commandline probably included you running a normal graphical session where the browser could be opened? Because cron doesn't have such a session, so I suspect it will not be run.
    – Nanne
    Jan 10, 2013 at 10:52
  • It is probably a better solution to try and find an extension for chrome or firefox that automatically refreshes a given tab every N minutes.
    – chronitis
    Jan 10, 2013 at 10:56
  • @Nanne, you are right. A friend of mine just got me the right answer for this on IM. All I need to do is prepend my command with export DISPLAY=:0 && in the cron. I have asked him to post the answer here so I can accept it and show gratitude by getting him karma-points :) Jan 10, 2013 at 11:01

4 Answers 4

12

Edit the crontab with export DISPLAY wrapper:

2 * * * * export DISPLAY=:0 && firefox %u

This opens an empty Firefox tab, you can use you favourite URL.

2
  • The best thing that worked for me was 0 */2 * * * export DISPLAY=:0 && /opt/google/chrome/chrome URL Jan 10, 2013 at 13:07
  • Didn't work for me: export DISPLAY=:0 && firefox %u Gtk-Message: 15:49:35.822: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it. Error: cannot open display: :0 Sep 6, 2023 at 18:50
7

cron is intended for command line jobs, and runs commands with a very limited set of environment variables - just HOME, LOGNAME, PATH, and SHELL.

At the least, chrome will require DISPLAY to be set. It may also be unhappy without other variables (eg, DBUS related ones). Run env in a terminal to see your current environment.

1
  • this answer provided the best learning. thanks. Jan 10, 2013 at 13:08
4

The enviroment that you get for a cronjob is very limited. For example you dont get the DISPLAY varible wich is required to open chrome.

Wrap your command in a script

#!/bin/sh
export DISPLAY=:0
/opt/google/chrome/chrome PARTICULAR_GOOGLE_DOC_URL

Not realy sure if you need more variables to get it to work. I made a similar script that started konsole (terminal in KDE) that worked in cron.

2
  • Not wrong at all, but a script was not needed, one liner in the crontab did it.. Jan 10, 2013 at 13:08
  • This worked best for me since my CRON task was kicking off a script! Thanks! Feb 4, 2017 at 19:16
-1

You can simply add setup your like this.

2 * * * * export DISPLAY=:0 && ,<do what ever you want.>

If this does not work, and you google-chrome or firefox not found, simply run the command below in your shell BASH, FISH, ZSH etc to get PATH.

echo $PATH

Whatever the result comes out from the above command just copy and paste it above your cronjob like this,

PATH=<result from cmd>

The reason of doing this, you might install the respective from snapd etc and that's why path is not defined as you downloaded from separate resource.

2
  • This is essentially a copy of the axisting anwers. Jul 2, 2022 at 7:32
  • I was facing the same issue but could not found any answers, in the by cumulative resources i complied this up so others won't have face which I had. Aug 20, 2022 at 15:40

You must log in to answer this question.

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