【问题标题】:Crontab python django issuecrontab python django问题
【发布时间】:2016-11-04 06:58:22
【问题描述】:

以下按预期工作:

python /usr/share/str8RED/manage.py getLiveResults

但是,当我使用以下 cronjob 时,没有任何反应:

*/1 * * * * python /usr/share/str8RED/manage.py getLiveResults

使用下面的链接,我设法创建了一个错误日志:

http://matthewwittering.com/blog/django-tips/running-a-django-management-commands-with-crontab.html

这告诉我:

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

我可以通过 echo "Hello World" 让 cronjab 每分钟都工作和运行。任何帮助将不胜感激,非常感谢,艾伦。

getLiveResults.py 的内容:

from django.core.management import BaseCommand
from straightred.models import StraightredTeam
from straightred.xmlsoccer import XmlSoccer


#The class must be named Command, and subclass BaseCommand
class Command(BaseCommand):
    # Show this when the user types help
    help = "My test command"

    # A command must define handle()
    def handle(self, *args, **options):


        xmlsoccer = XmlSoccer(api_key='XYZ', use_demo=False)
        teams = xmlsoccer.call_api(method='GetAllTeamsByLeagueAndSeason',
                                   seasonDateString='1617',
                                   league='English League Championship')

        numberOfTeamsUpdated = 0

        for team in teams:

            if '{http://xmlsoccer.com/Team}Team_Id' in team.keys():
                teamUpdate = StraightredTeam(teamid=team['{http://xmlsoccer.com/Team}Team_Id'],
                                             teamname=team['{http://xmlsoccer.com/Team}Name'],
                                             country=team['{http://xmlsoccer.com/Team}Country'],
                                             stadium=team['{http://xmlsoccer.com/Team}Stadium'],
                                             homepageurl=team['{http://xmlsoccer.com/Team}HomePageURL'],
                                             wikilink=team['{http://xmlsoccer.com/Team}WIKILink'],
                                             currentteam=1)
                teamUpdate.save()
                numberOfTeamsUpdated = numberOfTeamsUpdated + 1


        self.stdout.write("Hello world!")

【问题讨论】:

  • getliveResults 是做什么的?
  • 如果你使用的是虚拟环境,那么你需要激活环境,

标签: python django cron


【解决方案1】:

如果你使用的是虚拟环境,那么你需要激活环境,

可能是这样的:

*/1 * * * * /usr/share/str8RED/.env/bin/python /usr/share/str8RED/manage.py getLiveResults

【讨论】:

  • 我收到以下错误:回溯(最近一次调用最后一次):文件“/usr/share/str8RED/manage.py”,第 8 行,来自 django.core.management 的 import execute_from_command_line ImportError: No module named django.core.management
猜你喜欢
  • 2018-08-28
  • 2017-08-06
  • 2013-02-26
  • 2017-04-02
  • 1970-01-01
  • 2022-10-06
  • 2019-06-23
  • 1970-01-01
  • 2019-06-17
相关资源
最近更新 更多