python - Crontab python django 问题

标签 python django cron

以下内容按预期工作:

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

我可以让 cronjab 工作并每分钟运行一次 echo“Hello World”。任何帮助将不胜感激,非常感谢,艾伦。

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!")

最佳答案

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

也许是这样的:

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

关于python - Crontab python django 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40412272/

相关文章:

django - 为什么我的Django 1.3日志记录设置会导致所有消息输出两次?

jquery - 将图像发送到服务器 - 错误 415(不支持的媒体类型): "Unsupported media type\"image/png\"in request."

通过 cron 运行的 PHP 脚本不连接到数据库

来自 .txt 文件的 Python 列表

python - 在 Python 中实现 Web 服务的最佳方式是什么?

python - 在 Python 中将元组传递给 __init__

node.js - 使用 Google Cloud Platform 自动化 NodeJS 脚本

ruby - Rake 在 cronjob 中无法正常运行

python - Crontab 模式每年运行一次脚本/一生一次

python - Python中的空元组是 "constant"吗