Django 在 cron 中独立运行

发布于 2024-09-11 15:07:51 字数 2145 浏览 1 评论 0原文

我想在我的 crontab 中运行自动新闻通讯功能,但无论我尝试什么 - 我都无法使其工作。这样做的正确方法是什么? 这是我的 crontab 条目:

0 */2 * * * PYTHONPATH=/home/muntu/rails python2.6 /home/muntu/rails/project/newsletter.py

以及 newsletter.py 文件,该文件位于我的 django 项目的顶部文件夹中:

#! /usr/bin/env python
import sys
import os

os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
from django.core.management import setup_environ
from project import settings
setup_environ(settings)

from django.template.loader import get_template, render_to_string
from django.template import Context
from django.core.mail import EmailMultiAlternatives
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.conf import settings
from project.utilsFD.models import *
from django.http import HttpResponse, HttpResponseRedirect, Http404

def main(argv=None):
    if argv is None:
        argv = sys.argv

    template_html = 'static/newsletter.html'
    template_text = 'static/newsletter.txt'
    newsletters = Newsletter.objects.filter(sent=False)
    adr = NewsletterEmails.objects.all()
    for a in adr:
        for n in newsletters:
            to = a.email          
            from_email = settings.DEFAULT_FROM_EMAIL           
            subject = _(u"Newsletter - Method #1")

            text_content = render_to_string(template_text, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})
            html_content = render_to_string(template_html, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})

            msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            msg.content_subtype = "html"
            msg.send()
            n.sent = True
            n.save()

if __name__ == '__main__':
    main()

我做错了什么?当作为 django 应用程序运行时,该函数本身工作正常,没有任何问题,但是当我尝试从控制台运行它时,它给了我:

Traceback (most recent call last):
  File "newsletter.py", line 7, in <module>
    from project import settings
ImportError: No module named project

并且它根本无法从 cron 运行。

I want to run automatic newsletter function in my crontab, but no matter what I try - I cannot make it work. What is the proper method for doing this ?
This is my crontab entry :

0 */2 * * * PYTHONPATH=/home/muntu/rails python2.6 /home/muntu/rails/project/newsletter.py

And the newsletter.py file, which is located in the top folder of my django project :

#! /usr/bin/env python
import sys
import os

os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
from django.core.management import setup_environ
from project import settings
setup_environ(settings)

from django.template.loader import get_template, render_to_string
from django.template import Context
from django.core.mail import EmailMultiAlternatives
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.conf import settings
from project.utilsFD.models import *
from django.http import HttpResponse, HttpResponseRedirect, Http404

def main(argv=None):
    if argv is None:
        argv = sys.argv

    template_html = 'static/newsletter.html'
    template_text = 'static/newsletter.txt'
    newsletters = Newsletter.objects.filter(sent=False)
    adr = NewsletterEmails.objects.all()
    for a in adr:
        for n in newsletters:
            to = a.email          
            from_email = settings.DEFAULT_FROM_EMAIL           
            subject = _(u"Newsletter - Method #1")

            text_content = render_to_string(template_text, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})
            html_content = render_to_string(template_html, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})

            msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            msg.content_subtype = "html"
            msg.send()
            n.sent = True
            n.save()

if __name__ == '__main__':
    main()

What am I doing wrong ? The function itself was working without any problems when run as django app, but when I was trying to run it from console, it gave me :

Traceback (most recent call last):
  File "newsletter.py", line 7, in <module>
    from project import settings
ImportError: No module named project

And it does not work from cron at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-09-18 15:07:51

尝试将 cron 条目更改为:

0 */2 * * * cd /home/muntu/rails && python2.6 /home/muntu/rails/project/newsletter.py

这将确保“rails”目录位于 python 的路径中。如果要设置 PYTHONPATH,则创建一个 shell 脚本:

#!/bin/sh
export PYTHONPATH=/home/muntu/rails
python2.6 /home/muntu/rails/project/newsletter.py

并将该 shell 脚本放入 cron 条目中。

Try changing your cron entry to:

0 */2 * * * cd /home/muntu/rails && python2.6 /home/muntu/rails/project/newsletter.py

This will ensure that the "rails" directory is in python's path. If you want to set the PYTHONPATH, then create a shell script:

#!/bin/sh
export PYTHONPATH=/home/muntu/rails
python2.6 /home/muntu/rails/project/newsletter.py

and put the shell script in the cron entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文