Ubuntu – How to automaticly open a URL at specific times each day


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


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:

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?

Best Answer

The problem was already solved here.

Change the cron's crontab to launch your preferred Python interpreter with the script path as argument:

21 21 * * * /usr/bin/python /home/craig/openurl.py

Remember that a standard symbolic link will point to the default version of the interpreter, which may not be correct if your software is based on the 2.X syntax of Python.
The cron daemon may launch the interpreter with elevated rights, use a restricted interpreter for automated security-critical tasks.

EDIT to cover a bigger slice of problems after the cronjob setup:

To monitor in real-time the jobs done by cron and other system daemons, execute in a terminal:

tail -f /var/log/syslog


tailf /var/log/syslog

(on some systems the path of the log file is different, like /var/log/cron.log)

To redirect the OUTPUT of a cron job, insert a shell redirection for STDOUT in the cronjob file, like:

21 21 * * * /usr/bin/python /home/craig/openurl.py >> /home/craig/test.log

In a Python script a command like print('TEST') will append the string TEST to test.log; any simple subprocess will be also redirected.
Simply check the file to know if the script works as expected.

Please, feel free to comment under here if you have more questions and don't forget to press the left UP arrow and mark as Favorite if I'm of any help.

Have a nice day.