如何在python虚拟环境中运行cronjob?

编程入门 行业动态 更新时间:2024-10-27 14:33:41
本文介绍了如何在python虚拟环境中运行cronjob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

通常我通过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

Cron和virtualenv

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?

本文发布于:2023-11-24 03:00:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境   如何在   python   cronjob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!