Crontab fails to execute Python script

13,206

Solution 1

The main.py script calls some methods from other modules under python_prj, does that matter?

Yes, it does. All modules need to be findable at run time. You can accomplish this in several ways, but the most appropriate might be to set the PYTHONPATH variable in your crontab.

You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks.

[update] here is the top of my crontab:

www:~# crontab -l

DJANGO_SETTINGS_MODULE=djangocron.settings
PATH=...
PYTHONPATH=/home/django
MAILTO="[email protected]"
...
# m h  dom mon dow   command
10-50/10 * * * *               /home/django/cleanup_actions.py
...

(running cleanup actions every 10 minutes, except at the top of the hour).

Solution 2

Any file access in your scripts? And if so, have you used relative paths (or even: no explicit path) in your script?
When run from commandline, the actual folder is 'your path', where you start the script from. When run by cron, 'your path' may be different depending on environment variables.
So try using absolute paths to any files you access.

Share:
13,206
yebw
Author by

yebw

Updated on June 11, 2022

Comments

  • yebw
    yebw almost 2 years

    crontab fails to execute a Python script. The command line I am using to run the Python script is ok.

    These are solutions I had tried:

    • add #!/usr/bin/env python at the top of the main.py
    • add PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin at the top of crontab
    • chmod 777 to the main.py file
    • service cron restart

    my crontab is:

    PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
    
    */1 * * * * python /home/python_prj/main.py
    

    and the log in /var/log/syslog is:

    Nov  6 07:08:01 localhost CRON[28146]: (root) CMD (python /home/python_prj/main.py)
    

    and nothing else.

    The main.py script calls some methods from other modules under python_prj, does that matter?

    Anyone can help me?

    • Tjaart
      Tjaart over 11 years
      have you tried "/usr/bin/env/python /home/python_prj/main.py" in your cron command?
    • Tjaart
      Tjaart over 11 years
      Maybe your script is crashing before it can get anything done for whatever reason: lack of rights, executing in the wrong cwd?
    • yebw
      yebw over 11 years
      I tried /usr/bin/env/python, still no luck
    • jfs
      jfs over 11 years
      redirect the output from the script to a file command >> /path/to/output.log 2>&1 otherwise check email that cron sends.
    • yebw
      yebw over 11 years
      thanks for your reply, i tried to add the output.log, but there is nothing in this file.
  • yebw
    yebw over 11 years
    I dont understand, I type "python /home/python_prj/main.py" in the command line, it works. PYTHONPATH will be set to python'path or the modules' path
  • yebw
    yebw over 11 years
    all the modules under the python_prj are customized
  • yebw
    yebw over 11 years
    I just removed all the modules the main.py imported. still no work
  • atelcikti1
    atelcikti1 over 11 years
    What do you mean by "still no work"? How do you know that it is working when you run it from the command line? Have you tried creating a minimal script, e.g. "print 'hello world'" (or even better: "import sys; print sys.path") to verify that you get exception emails or can redirect output to a file? Scripts ran under cron start with a different shell (/bin/sh) and an empty environment, so just because something works on the command line doesn't mean it will work under cron. ps: the shebang line will not be used when you put "python scriptname.py" in your crontab...
  • atelcikti1
    atelcikti1 over 11 years
    one more thing... programs ran under cron doesn't have a useful working directory either -- have you tried running your script from the command line, but from a directory completely unrelated to your project? If you're running it from the python_prj directory, imports will work even if you don't have e.g. a init.py file...
  • yebw
    yebw over 11 years
    really pleaure for your help, and sorry for my poor English. I am a newer at the cron and python. Can you tell me how to set the PYTHONPATH?
  • Emo Mosley
    Emo Mosley over 11 years
    @yebw - when cron runs a scheduled task, it executes in its own environment with its own PATH and environment variables, such as PYTHONPATH that thebjorn has mentioned. The path and environment will differ from what your command prompt will have. This is arranged that way for security purposes.
  • vrleboss
    vrleboss about 7 years
    helpful answer: I was accessing a sqlite database in my python script and had not provided full path.