如何在python虚拟环境中运行cronjob? [英] How do I run a cronjob with a python virtual environment?
问题描述
通常我通过SSH进入我的EC2实例并运行:
Usually I SSH into my EC2 instance and run:
source MYVENV / bin / activate
我如何设置cronjob激活虚拟环境?我的Django脚本需要存储在〜/ .bash_profile
How do I set my cronjob to activate the virtual environment? My Django script requires ENVIRONMENT variables that are stores in ~/.bash_profile
我尝试按照此处的步骤无效
I tried following the steps here to no avail
SHELL=/bin/bash
*/1 * * * * root source /home/ec2-user/MYVENV/activate && python /home/script.py
这是我目前在上面的设置。
This is my current setup above.
我在日志中得到以下错误:
I get this following error in the log:
/ bin / bash:root:找不到命令
/bin/bash: root: command not found
推荐答案
创建一个shell脚本
,例如scripts.sh
create a shell script eg scripts.sh
#!/bin/bash
source /home/user/MYVENV/bin/activate
python /path/to/file/script.py
然后放入cron
*/1 * * * * bash /path/to/shell/script/scripts.sh
脚本将加载您所有的环境变量并从您环境中的python执行
The script will load all your environment variables and execute from the python in your environment
这篇关于如何在python虚拟环境中运行cronjob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!