Python社区  »  Django

django crontab未执行django自定义命令

Negiiii • 7 月前 • 108 次点击  

我使用django corntab在特定时间间隔后执行django命令。我在为玉米作业定义django设置时遇到问题。我阅读了django corntab的doc并提到了django命令类的路径,但它不起作用。

管理/命令/发送电子邮件.py

from django.core.management.base import BaseCommand, CommandError
from django.core.mail import EmailMessage

class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        try:
            msg = EmailMessage('Hi! I am using Cron!',
                           'I am using django commands and cron for scheduled tasks.', to=['***.negi14@gmail.com'])
            msg.send()
            self.stdout.write(self.style.SUCCESS('Successfully sent email'))
         except Exception:
             raise CommandError('Sorry! something went Wrong!')

设置.py

INSTALLED_APPS = [
....
# third party packages
'django_crontab',
# local apps
'mynewapp',]

CRONJOBS = [
('*/1 * * * *', 'mynewapp.management.commands.send_email.Command', '>> '+os.path.join(BASE_DIR,'log/mylogfile.log 2>&1'))]

python manage.py crontab添加

adding cronjob: (08bf4cc61f82eb0b6102bd6c8e9d953f) -> ('*/1 * * * *', 'mynewapp.management.commands.send_email.Command', '>> /Users/ishan/PycharmProjects/newproject/log/mylogfile.log 2>&1')

python manage.py crontab显示

Currently active jobs in crontab:
08bf4cc61f82eb0b6102bd6c8e9d953f -> ('*/1 * * * *','mynewapp.management.commands.send_email.Command', '>> /Users/ishan/PycharmProjects/newproject/log/mylogfile.log 2>&1')
You have new mail in /var/mail/ishan
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52891
 
108 次点击  
分享到微博