Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python crontab and paths

I have a Python driver and library scripts that are siblings:

/home/mydir/pythonProjs/
  • driver.py

  • lib.py

In driver.py I have the line:

from lib import method1

The following is successful from my command line on Linux:

python /home/mydir/pythonProjs/driver.py

But when I try the following in crontab:

10 1 * * * export PYTHONPATH=~/mydir/pythonProjs; python /home/mydir/pythonProjs/driver.py

I get the error:

ImportError: No module named lib.method1

I have also attempted changing path setting in my crontab command to the fully-qualified path /home/mydir/pythonProjs, omitting the 'export', and have also attempted writing .sh files (with the necessary #!bin/bash...)

I have one main question and a follow-up question: main: What is a best practice way to fix my problem? follow-up: What is the philosophy behind cron having different path access than my shell?

Before I get down voted too quickly, I will mention that I have read but have not been successful (or correctly parsed) the following: - Where can I set environment variables that crontab will use? - Crontab Issues running Python - http://pythonadventures.wordpress.com/2012/03/31/calling-a-python-script-from-crontab/

like image 352
Quetzalcoatl Avatar asked Mar 18 '23 14:03

Quetzalcoatl


1 Answers

Try to print out environment variables from a dummy job

* * * * * env > /tmp/env.output

as suggested in https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work

Also check what shell crontab is using. You can set the $SHELL environment variable to bash by adding a line

SHELL=/bin/bash

at the beginning of the crontab file.

like image 55
user2314737 Avatar answered Mar 31 '23 23:03

user2314737