6

Am trying to create a cron job to open a web page at a scheduled time.

I have edited my cron using crontab -e and append my job. I wrote my job in a script like this: xdg-open (URL) I saved this script in my /home/flicker directory.

However, when I edit my cron (crontab -e), it saves the file as /tmp/crontab.tIzXBv/crontab.

After saving it successfully says installing new crontab.

I made my file script executable, but still failing to handle the job. I wanted the script to run at 14:30PM, so I added this line to my crontab:

30 14 * * * /home/flicker/open-web.sh

This is still failing to work please help. Here is my script below:

#!/bin/bash
xdg-open https://www.google.com
5
  • 3
    Are you setting the display in your script export DISPLAY=:0? Also, what exactly is failing, nothing happens, or you get an error?
    – user508889
    Jun 1, 2016 at 13:02
  • @bc2946088, an not get any error, nothing happens, I want it to open in Mozilla Firefox. Jun 1, 2016 at 14:32
  • Does your script work if you run it manually from Terminal?
    – dufte
    Jun 2, 2016 at 12:29
  • 1
    Please add your entire script to your base-post/question above
    – dufte
    Jun 2, 2016 at 12:30
  • @dufte, the script works when I run it manually Jun 2, 2016 at 14:09

1 Answer 1

11

to execute a bash script via cron i would use

30 14 * * * /bin/bash /home/flicker/open-web.sh >/dev/null 2>&1

To call a web page/URL via cron & curl

30 14 * * * /usr/bin/curl  http://funkyname.com/blub.html >/dev/null 2>&1

Update:

After realizing you want to open an actual browser and NOT just calling a script - i guess i understood your case.

You can solve that without bash script - instead write all logic directly to your crontab.

for Chrome:

30 14 * * * export DISPLAY=:0 && google-chrome --new-window http://www.randomurl.com

for Firefox:

30 14 * * * export DISPLAY=:0 && firefox --new-window http://www.randomurl.com

Keep in mind:

The --new-window parameter is forcing the browser in charge to open a new window, if you don't want that, just remove that section.

To avoid noise output you can optional add >/dev/null 2>&1 at the end of your cron entry.

5
  • thank you so much for your suggestions, unfortunately it didn't work still. Jun 2, 2016 at 14:45
  • 1
    updated my post, tested it - is working now
    – dufte
    Jun 3, 2016 at 6:00
  • Wow! wow! @dufte, thanks alot, your update answer, it has worked, thanks alot Jun 14, 2016 at 9:39
  • 1
    Glad to hear it finally worked out for you
    – dufte
    Jun 14, 2016 at 9:58
  • when removing the --new-window firefox fails to open because is already running... I would like to open in a new tab. It works fine if called directly on terminal, but not as cron job. Firefox message: Firefox is already running, but is not responding. To use Firefox, you must first close the existing Firefox process, restart your device, or use a different profile. Sep 8, 2022 at 23:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.