Crontab issues with python script

2019-09-15 11:19发布

问题:

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.

回答1:

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