0

I have a script that runs perfectly from command line:

/home/anaconda2/bin/python /project_folder/script.py

I added some details below:

$ echo $PYTHONPATH
:/project_folder/

$ which python
/home/anaconda2/bin/python

Which runs the script perfectly, flawlessly.

Then from within crontab (which has been successful for another script that didn't have local import issues) I can't get the script to run.

Crontab of code that doesn't work:

PYTHONPATH=/project_folder
* * * * * /home/anaconda2/bin/python /project_folder/script.py

Nothing happens. It's killing me and much of my time trying to figure this one out - any help much appreciated.

SteelyDanish
  • 629
  • 2
  • 8
  • 15
  • 1
    Make sure any environment variables your script needs are defined. Cron doesn't run all the same start up scripts an interactive login runs. – GreenMatt Mar 16 '17 at 00:13
  • There are some useful tips here for crons that silently fail: http://unix.stackexchange.com/questions/207/where-are-cron-errors-logged – Russel Simmons Mar 16 '17 at 00:22
  • How would I go about figuring that out @GreenMatt (thanks btw) – SteelyDanish Mar 16 '17 at 00:22
  • 1
    Not sure what you're trying to figure out: That cron doesn't run all the start up scripts your interactive login does? That comes from reading the docs, which usually comes from trying to figure out why your script does not run from cron! ;-) What environment variables are needed? Trial and error or examining your start up scripts are how I usually figure it out. Also, cron may be emailing results or logging them. I would suggest looking at your .bashrc (or equivalent) to see if you're making changes to your PATH or PYTHONPATH variables as a starting point. – GreenMatt Mar 16 '17 at 00:29
  • I noticed this line (at end of comment) in my bashrc which was the only env variable I saw - however I am not sure how to include that into crontab >>> export PATH="/var/anaconda2/bin:$PATH" – SteelyDanish Mar 16 '17 at 00:45
  • 1
    @SteelyDanish: One approach is to have cron call a Bash wrapper script which sets up the PATH and then runs your Python script. Alternatively, there are ways to set up the environment from with a Python script; have a look at: http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python – GreenMatt Mar 16 '17 at 01:28
  • @GreenMatt - thanks did that it worked. Posting answer shortly – SteelyDanish Mar 16 '17 at 17:57

1 Answers1

0

I resolved it via creating a wrapper shell script. Ugly in that i'm exporting the python path each time, but it works.

#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/project_folder"
source ~/.bash_profile
cd /project_folder && /my/anaconda/location/bin/python /project_folder/cript.py
SteelyDanish
  • 629
  • 2
  • 8
  • 15