run a crontab job using an anaconda env

I recently switched from canopy 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 miniconda 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.

Leave a Comment