32

I need to run a Java applet from a Firefox. The problem is, I need to run this Firefox from a cron. If I run from the command line and its work fine, but when put the command in the cron it failed:

*/1 * * * * firefox http://domain.com/iqms/packetloss/iqmspacket2.html
Jacob Vlijm
  • 83,767
fadabi Nawi
  • 421
  • 1
  • 4
  • 11

3 Answers3

42

Cron runs in an environment that knows nothing about the display manager ($DISPLAY isn't set). Your terminal window that you've been testing in has this set for it. If you tried it in a "real" terminal, or over SSH, you'd see it break.

If you've only got one X server running, just specifying the display might work:

* * * * *    DISPLAY=:0 firefox ...

You could probably put more effort into working out what display it's sitting under rather than assuming it's :0 but that will probably work most of the time.

If you just need to reload something a lot, look at Tab Auto Reload or similar Firefox plugins.

Oli
  • 293,335
  • I found this answer but still does not start the firefox. After a few search, I found that i need to run as a valid user instead from root. * * * * * "DISPLAY=:0.0 /usr/bin/firefox -new-window http://x.com/iqms/packetloss/iqmspacket2.html" – fadabi Nawi Aug 22 '14 at 00:35
  • 4
    @fadabiNawi An easier way of managing that would be to install the cron line into the correct user's crontab. Log in as who you want, and crontab -e – Oli Aug 22 '14 at 06:56
  • you just read my mind 6 years before – ram0nvaldez Dec 05 '20 at 14:07
  • This seemed to work on Wayland for me. – Flimm Feb 14 '23 at 11:02
10

I found this answer i am looking for addition to oli's :

After a few search, I found that i need to run as a valid user instead from root.

* * * * * su fadabi -c "DISPLAY=:0.0 /usr/bin/firefox -new-window x.com/iqms/packetloss/iqmspacket2.html"; 

tq

fadabi Nawi
  • 421
  • 1
  • 4
  • 11
  • This helped, thanks a lot! Just a note: full path to Firefox is mandatory, I didn't realise that and it didn't work – ᴍᴇʜᴏᴠ Jul 27 '18 at 07:28
  • It would be easier to run the command from the user's personal crontab. Edit the user's crontab by running crontab -e without sudo. – Flimm Feb 14 '23 at 11:01
7

For me DISPLAY=:0 didn't work. When I check echo $DISPLAY in the terminal it returned 1. So DISPLAY=:1 worked for me. (on Ubuntu 20.04)

Senduran
  • 71
  • 1
  • 1