run a crontab job using an anaconda env

隐身守侯 提交于 2019-12-03 03:59:07

问题


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.


回答1:


Don't call sh but bash. source is a bash command.

- sh scripts/my_script.bash
+ bash scripts/my_script.bash

Or just

chmod +x scripts/my_script.bash
./scripts/my_script.bash

since you added the bash shebang.




回答2:


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.




回答3:


In my case, I got this error when I ran this line of shell script: source activate my_env

activate: No such file or directory

So I changed source activate my_env to source /path/to/conda/bin/activate my_env. Then it starts working.



来源:https://stackoverflow.com/questions/36365801/run-a-crontab-job-using-an-anaconda-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!