You are not logged in.

#1 2008-06-04 08:25:04

Baxter
Member
Registered: 2006-11-27
Posts: 33

Django cron help?

I'm trying to run a modified version of daily_cleanup every night.  In my crontab I have:

01 23 * * *  /home/grmadmn/webapps/django/django/bin/daily_cleanup.py

The job itself:
#!/usr/local/bin/python2.5
import sys, os
sys.path.append('/home/grmadmin/webapps/django')
sys.path.append('/home/grmadmin/lib/python2.5/django/bin')
sys.path.append('/home/grmadmin/lib/python2.5/django/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'classic.settings'

import datetime
from django.core import mail
from django.db import transaction
from django.contrib.sessions.models import Session
from registration.models import RegistrationProfile

def clean_up():
    """Clean up expired sessions."""
    Session.objects.filter(expire_date__lt=datetime.datetime.now()).delete()
    transaction.commit_unless_managed()
    RegistrationProfile.objects.delete_expired_users()
    mail.send_mail("GRM daily cleanup run", #subject
        "The Cron job ran daily cleanup", #message
        "*from email address*",
        ["*to email address*"],
        fail_silently = False
    )

if __name__ == "__main__":
    clean_up()

I'm pretty sure it's not running. Any suggestion. In the actual script, the from and to emails are there, of course.

Offline

 

#2 2008-06-04 11:29:42

Zigiless
Member
Registered: 2008-02-22
Posts: 10

Re: Django cron help?

I believe you need to set two environment variables, for your project folder and one for your Django settings file.
You can put them in several places, including in crontab itself (which is what I am doing).
Add this to the start of your crontab:

Code:

PYTHONPATH=/home/(user)/webapps/django/:/home/(user)/lib/python2.5
DJANGO_SETTINGS_MODULE=(site).settings

Offline

 

#3 2008-06-06 12:52:26

Baxter
Member
Registered: 2006-11-27
Posts: 33

Re: Django cron help?

Nope. That didn't seem to do it.

Offline

 

#4 2008-06-06 19:58:45

stranger1
Member
Registered: 2008-03-02
Posts: 19

Re: Django cron help?

I think your format in crontab is wrong. you should invoke python interpreter to run the daily_cleanup.py

Try

01 23 * * * /usr/local/bin/python2.5 /home/grmadmn/webapps/django/django/bin/daily_cleanup.py

Offline

 

#5 2008-06-06 20:23:04

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Django cron help?

A couple of things here.. all of these folks are suggesting Python2.5 stuff, but if your environment is using Python 2.4, make sure you take that into account. You definitely need the PYTHONPATH set, either in your .bashrc  or in the crontab as Zigiless suggested, but make sure that it is pointing to where your applicable django libraries are actually installed.

Also, and more importantly, make sure that your crontab entry is pointing to the correct location for your script. I'm looking in your home directory right now, and I don't think your script is where your cron job thinks it is.

Finally, if your script produces any sort of output, you can direct it to a file to see what's going on, eg:

01 23 * * *  /home/grmadmn/webapps/django/django/bin/daily_cleanup.py >> /home/grmadmn/cron.out 2>&1

Hope that helps!

Offline

 

#6 2008-06-13 11:33:05

Baxter
Member
Registered: 2006-11-27
Posts: 33

Re: Django cron help?

Doh! It sure helps to actually point it to the right script!

Offline

 

#7 2008-06-16 10:50:33

Baxter
Member
Registered: 2006-11-27
Posts: 33

Re: Django cron help?

Well, still no luck with this. Messing around with it a bit more, I'm seeing ImportError: No module named django.core

Which would lead me to believe I've got some sort of path issue somewhere. The cron is copied from another site (also on webfaction) that DOES work, so I'm just not sure what I'm missing here. The job again:

#!/usr/local/bin/python2.5
import sys, os
sys.path.append('/home/grmadmin/webapps/django')
sys.path.append('/home/grmadmin/lib/python2.5/django/bin')
sys.path.append('/home/grmadmin/lib/python2.5/django/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'grm.settings'

"""
Daily cleanup job.

Can be run as a cronjob to clean out old data from the database
Currently cleans out expired sessions and expired registration profiles.
"""
import datetime
from django.core import mail
from django.db import transaction
from django.contrib.sessions.models import Session
from registration.models import RegistrationProfile

def clean_up():
    """Clean up expired sessions."""
    Session.objects.filter(expire_date__lt=datetime.datetime.now()).delete()
    transaction.commit_unless_managed()
    RegistrationProfile.objects.delete_expired_users()
   ### send mail ####

if __name__ == "__main__":
    clean_up()

Offline

 

#8 2008-06-17 16:34:17

qmanic
Member
Registered: 2008-06-13
Posts: 3

Re: Django cron help?

If /home/grmadmin/lib/python2.5/django/ is actually the django module (i.e. there isn't a subfolder called django in it), try changing

sys.path.append('/home/grmadmin/lib/python2.5/django/')

to

sys.path.append('/home/grmadmin/lib/python2.5/')

and see if that gets you anywhere.

Offline

 

#9 2008-06-18 11:22:19

Baxter
Member
Registered: 2006-11-27
Posts: 33

Re: Django cron help?

It did. Thanks

Offline

 

#10 2009-07-30 16:06:43

redseam
Member
From: Alpine, UT
Registered: 2008-10-02
Posts: 13
Website

Re: Django cron help?

None of this helped for me. I finally found a clue in my wsgi file:

sys.path = ['/home/mylogin/webapps/mysite', '/home/mylogin/webapps/mysite/lib/python2.5'] + sys.path

I replaced the other sys.path.append lines in the script with the above and finally got it to work.


http://inzolo.com - Budget Happy!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson