0

I am using kubuntu 14.04. I have installed cron using sudo apt-get install cron, and then I created this file in IDLE, called openurl.py.

#!/usr/bin/env python

import webbrowser

webbrowser.open('http://eample.com')

I then typed chmod +x openurl.py into the terminal to make the .py file excecutionable. If I type in./openurl.py to the terminal, the script works.

then, using the kickoff application launcher I clicked system settings > task scheduler > new task > then I searched for the openurl.py file, and selected when I wanted it to run.

If I type crontab -e into the terminal, this is displayed:

#openurl
21 21 * * *     /home/craig/openurl.py


# File generated by KCron the Thursday 29 Jan 2015 21:20.

And then I wait, and nothing happens. What am I doing wrong?

3
  • Please would you clarify what you mean by "the script works". What actually happens? Jan 29, 2015 at 22:02
  • The web page opens in my default browser, Firefox.
    – Blue7
    Jan 29, 2015 at 22:03
  • Your cron script doesn't know anything about your GUI (and therefore your web browser), so it can't tell firefox to open the page. Jan 29, 2015 at 22:30

3 Answers 3

1

I think your cron configuration is correct. I'm not completely familiar with the python webbrower.open() function, but I assume that you are expecting the python script to open your page in a visible browser window.

I think what's happening here is that when you call the script from the command line it works because it's running within your graphical session. When it is called from cron it is run in a new session without the hooks into your current graphics environment so it has nowhere to launch the browser. You can verify that the script is running by having it create a file with the current timestamp or write the current timestamp into a log somewhere.

If your ultimate goal is to have a page refresh once a day you may look into a browser plugin to achieve that, otherwise you'll need to figure out the environment issue.

2
  • Thanks for your answer. Do you have any suggestions as to how I could "hook" the script to my current graphics environment?
    – Blue7
    Jan 29, 2015 at 22:16
  • Most of my work is with headless Linux servers, so I don't know much about interfacing with the graphics environment. I'm not even 100% sure that it's possible to give cron access to it. I assume the current graphics environment is defined in some environment variable. Try issuing the "printenv" command to see what your current environment variables are. You'll have to explicitly set them for cron. There are lots of online resources for that though. Jan 29, 2015 at 22:22
0

The problem is cron does not know about your login session.

If the script does not work from the system console (ctrl-alt-f1) it definately woen work from cron.

If you want to automate GUI stuff, it's probably bes to use a GUI-base dscheduler instead of cron.

0

As Andrew said, your job might failing because it requires an X session, since you're trying to open a web browser. You should place export DISPLAY=:0; after the schedule in your cronjob, as in

21 21 * * * export DISPLAY=:0; /home/craig/openurl.py

If this doesn't work, you could try replacing :0 with the output of echo $DISPLAY in a graphical terminal.

You must log in to answer this question.

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