python - 用于发送邮件的 crontab

标签 python linux django cron

当我使用时,我使用 django 创建了 send_newsletter 管理命令

python manage.py send_newsletter

它有效。 我的 send_newsletter.py

sys.path.append('/srv/apps/')  
sys.path.append('%s/actecil_newsletter' % os.getcwd())  
sys.path.append('%s/actecil_newsletter/actecil_newsletter' % os.getcwd())  
sys.path.append('/srv/apps/apps_django_1_4') 
sys.path.append('/srv/apps/apps_django_1_4/Django-1.4.3') 
if 'DJANGO_SETTINGS_MODULE' not in os.environ.keys():
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

class Command(BaseCommand):
  args = '<mail_id, mail_id, ...>'
  def handle(self, *args, **options):   
    mail = Mail.objects.all()
    for mail in mail:
        print "------------------------------------------------------------------------------------"
        if not mail.sended :
            soup = BeautifulSoup(mail.content)
            subject = mail.subject
            recipients = []
            for t in mail.tags.all():
                for r in t.recipients.all():
                    if not r in recipients:
                        recipients.append(r)

            for r in mail.recipients.all():
                if not r in recipients:
                    recipients.append(r)

            if recipients:
                for a in soup.find_all('a'):
                    link = Link()
                    link.link = (a.get('href'))
                    link.mail = mail
                    link.save()
                    a['href'] = "%s/compagnes/link/%s/%s/%s/" % (settings.WEBSITE_URL, mail.id, '-*{^@+*@=)=-+*-=+*-=*+=--=*-*=-==)@@###/+@]}-*', link.id)
                tag = soup.new_tag('img', src="%s/compagnes/mail/%s/%s/" % (settings.WEBSITE_URL, mail.id, '-*{^@+*@=)=-+*-=+*-=*+=--=*-*=-==)@@###/+@]}-*'))
                soup.body.insert_after(tag)
                content = soup.prettify()
                for r in recipients:
                    c = content.replace('-*{^@+*@=)=-+*-=+*-=*+=--=*-*=-==)@@###/+@]}-*', "%s" % r.id)
                    msg = EmailMultiAlternatives(subject.encode('utf-8'), c.encode('utf-8'), settings.DEFAULT_FROM_EMAIL, [r.mail, ])
                    msg.attach_alternative(c, "text/html")
                    msg.send()
            mail.sended = True      
            mail.date_sent = datetime.datetime.now()
            mail.save()

我用

创建了一个 cron
sudo crontab -e

在终端上并将其放入 crontab 文件中:

*/5 * * * * cd /srv/apps/newsletter/actecil_newsletter && python manage.py send_newsletter

但是它不起作用,如果有人知道如何解决它?

编辑:我在 crontab 日志中收到此错误

Traceback (most recent call last):
File "manage.py", line 9, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

最佳答案

问题是在 cron 中运行的 python 无法访问 django 安装。对此有很多问题,Django 是如何安装在你的系统中的? (使用 SO 包、pip 或手动...)。

使用django-cron将使您的生活变得更加轻松,简化您的开发并以 django 方式管理您的所有应用程序;)

提示:

  • 在 Django 开发中,您必须使用 virtualenv
  • 考虑阅读有关任务/队列作业的内容,例如 Celery

关于python - 用于发送邮件的 crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137763/

相关文章:

python - 使用 python 更新 Firebase 中的单个值

python - Pyserial 缓冲区填充速度快于我的阅读速度

linux - Chef - 如何使用 UUID 挂载 LVM

linux - NodeJS 脚本无法在 Windows 或 Linux 上运行

c - 指向结构数组的指针

django - 检查Django组是否有权限

python - 如何在 Django 模板中使用 Ajax?

python - 从python 3中的文件路径列表中删除重复的文件

python - 如何将这两条 3D 线与 Python 的 matplotlib 中的表面连接在一起

python - 如何从 Wagtail CMS 中的 streamfield 检索页面内容?