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.

7
  • 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. Commented Mar 16, 2017 at 0:13
  • There are some useful tips here for crons that silently fail: unix.stackexchange.com/questions/207/… Commented Mar 16, 2017 at 0:22
  • How would I go about figuring that out @GreenMatt (thanks btw) Commented Mar 16, 2017 at 0: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. Commented Mar 16, 2017 at 0:29
  • 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: stackoverflow.com/questions/5971312/… Commented Mar 16, 2017 at 1:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.