Alan Tingey Alan Tingey - 1 month ago 16
Python Question

Crontab python django issue

The following works as expected:

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


However, nothing happens when I use the following cronjob:

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


Using the link below I have managed to create a error log:

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

This informs me that:

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


I can get cronjab working and running every minute with echo "Hello World". Any help would be appreciated, many thanks, Alan.

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

Answer

If you are using virtual env, then you need to activate the environment,

maybe something like:

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