预期的工作如下:
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
我可以通过回声“ 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!")
最佳答案
如果您使用的是虚拟环境,则需要激活环境,
也许像这样:
*/1 * * * * /usr/share/str8RED/.env/bin/python /usr/share/str8RED/manage.py getLiveResults
关于python - Crontab python Django问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40412272/