run a crontab job using an anaconda env

stoebelj picture stoebelj · Apr 1, 2016 · Viewed 13.7k times · Source

I want to have a cron job execute a python script using an already existing anaconda python environment called my_env. The only thing I can think to do is have the cron job run a script called my_script.bash which in turn activates the env and then runs the python script.

#!/bin/bash
source activate my_env
python ~/my_project/main.py

Trying to execute this script from the command lines doesn't work:

$ sh scripts/my_script.bash
scripts/my_script.bash: 9: scripts/my_script.bash: source: not found

What do I need to do to make sure the proper environment is activated. Its ok to explain it to me like I'm 5.

Answer

Eric Bridger picture Eric Bridger · Apr 3, 2016

I recently switched from to Anaconda precisely to get away from having to activate an env in cron jobs. Anaconda makes this very simple, based on the PATH enviornment variable. (I'm using not the full Anaconds install but I believe anaconda should work the same way)

There are two different approaches, I've tested;

  • Add a shebang in your python script, main.py

    #!/home/users/user_name/miniconda2/envs/my_env/bin/python

  • Add PATH to the top of your crontab

    PATH=/home/users/user_name/miniconda2/envs/my_env/bin

Update:

Jérôme's answer and cbarrick's comments are correct. I just got burned using the above approach in a Conda env which needed pynco, which needs the full conda environment to find proper the nco commands, such as ncks, ncrcat. Solved by running a bash script from cron which calls conda activate first.