2016-11-03 76 views
1

預期以下工作:crontab的蟒蛇Django的問題

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

使用下面的鏈接,我已經成功地創建一個錯誤日誌:

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

然而,當我用下面的cronjob沒有任何反應:

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的工作和運行與之呼應的 「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!") 
+0

什麼'getliveResults'呢? –

+0

如果您使用的是虛擬環境,那麼您需要激活環境, –

回答

1

如果您正在使用虛擬ENV,那麼你需要激活環境,

也許是這樣的:

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

我收到以下錯誤: 回溯(最近調用最後一次): 文件「/usr/share/str8RED/manage.py」,第8行,在 from django.core.management import execute_from_command_line ImportError:沒有名爲django.core.management的模塊 –

+0

一旦我做得正確,就完美了lol。謝謝。 –