为我的Django应用程序设置crontab

为我的Django应用程序设置crontab

本文介绍了为我的Django应用程序设置crontab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为Django应用程序设置crontab的问题上花了一周的时间,但我几乎发现了同样的问题.(问题链接到无法使用Django-cron进行函数调用)

I had an issue with setting up crontab for my Django application for a week and I have almost figured out the same. (Issue linked with Unable to make a function call using Django-cron)

我的crontab -e语法是

My crontab -e syntax is

* * * * * /Users/ashwin/dashboard/proj_application/exec.sh >> /Users/ashwin/dashboard/proj_application/data.log 2>&1

在我的exec.sh中,我有

And in my exec.sh, I have

#!/bin/bash
cd "$(dirname "$0")";
CWD="$(pwd)"
echo $CWD
python -c 'import proj_application.cron as cron; cron.test()'

cron.py 中,

from django.core.mail import send_mail
from smtplib import SMTP
from email.mime.text import MIMEText
import datetime

def test():
    message = "<p>This is test mail scheduled to send every minute</p>"
    my_email = MIMEText(message, "html")
    my_email["From"] = "[email protected]"
    my_email["To"] = "[email protected]"
    my_email["Subject"] = "Title"

    sender = "[email protected]"
    receivers = ["[email protected]"]

    with SMTP("localhost") as smtp:
        smtp.login(sender, "yyy@1234")
        smtp.sendmail(sender, receivers, my_email.as_string())

实际问题:

现在crontab能够调用exec.sh文件,并且我能够在回显中打印$ CWD,当调用到 cron.py 时,它无法识别django.core.mail并引发以下错误.

The crontab is now able to call the exec.sh file and I am able to print $CWD in echo, when the call comes to cron.py, It is unable to recognize django.core.mail and throws the following error.

from django.core.mail import send_mail
ImportError: No module named django.core.mail

我认为,我需要在某个地方设置虚拟环境或python变量路径,但是由于我是crontab的新手,所以我不确定该怎么做.

I think, I need to set up virtual environment or python variable path somewhere, but since I am new to crontab, I am not sure how to do that.

感谢您的协助.预先感谢.

Any assistance is appreciated. Thanks in advance.

推荐答案

是的,您可能需要使用虚拟环境(尽管它是可选的,但是是最佳实践).

yes you are correct you may need to use virtual environment(although its optional but best practice).

要创建虚拟环境(希望先安装python)

To create virtual environment(expects python to be installed prior)

python -m venv venv

要激活(路径可能因操作系统而异(我的操作系统是Windows)

To activate (path may differ based on OS(mine is windows))

source venv/Scripts/activate

要在虚拟环境中安装依赖项

To install dependency in virtual environment

pip install Django

现在大家都准备使用虚拟环境的Django.

Now you all set to use virtual env's Django.

哪个python 应该映射到venv路径.

which python should map to venv path.

现在您有两种方法来运行python脚本#1使用直接python路径

Now you have two ways to run python script#1 use direct python path

absolute/path/of/venv/bin/python3 -c 'import proj_application.cron as cron; cron.test()'

#2激活虚拟环境并以相同的方式使用bash脚本.

#2 activate virtual environment and use the bash script as same.

#!/bin/bash
cd "$(dirname "$0")";
CWD="$(pwd)"
echo $CWD
source venv/Scripts/python   # this will be in windows
python -c 'import proj_application.cron as cron; cron.test()'

这篇关于为我的Django应用程序设置crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:11