如何使用 python 虚拟环境运行 cronjob? [英] How do I run a cronjob with a python virtual environment?
问题描述
通常我通过 SSH 连接到我的 EC2 实例并运行:
Usually I SSH into my EC2 instance and run:
源 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 中放置
Then in cron put
*/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屋!