1

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?

4
  • How often do you want this crontab to run?
    – geoffmcc
    Jan 29, 2015 at 22:35
  • Every day, at the same time
    – Blue7
    Jan 29, 2015 at 22:36
  • K. Just checking, cause unless I'm doing the math wrong this will be every day @ 9:21pm
    – geoffmcc
    Jan 29, 2015 at 22:37
  • That is correct :)
    – Blue7
    Jan 29, 2015 at 22:42

2 Answers 2

4

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

or

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.

9
  • 1
    Or better yet, click the check to mark as accepted.
    – geoffmcc
    Jan 29, 2015 at 22:34
  • 2
    An explicit call to the interpreter is suggested, because the interpretation of the Magic Number in a script may be different for each shell: an update of the default shell can cause an unpredictable behaviour. The best thing to do is call the interpreter with a verbose/extended syntax and absolute paths. This is a valid suggestion for every *nix system and will help in every administrative task. Jan 29, 2015 at 22:41
  • Thank you for your answer, this is all very helpful. I have a few follow up questions though if that is okay? I only started using Linux today so I am a complete beginner. What is the cron's crontab?
    – Blue7
    Jan 29, 2015 at 22:47
  • @LorenzoAncora I changed the cron script to 57 22 * * * /usr/bin/python /home/craig/openurl.py but it still doesn't work.
    – Blue7
    Jan 29, 2015 at 22:56
  • @Blue7 a crontab is a configuration file used by cron and affine daemons to execute system and user tasks, it requires an automated procedure to be installed, modified ecc. and should have correct permissions on the file-system. The crontab utility does all for you, except the configuration, for which you MUST read the manual (Unix way...). Jan 29, 2015 at 23:20
1

I faced the same problem as you. Your job might be failing because it requires an X session, since you're trying to open a web browser. You should put export DISPLAY=:0; after the schedule in your cronjob, as in

21 21 * * * export DISPLAY=:0; /usr/bin/python /home/craig/openurl.py >> /home/craig/test.log

 python3 /home/fin/Labs/my-cheatcodes/src/dsktp_startup_script/dsktp_startup_script.py > capture_report.txt

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 .