You are not logged in.
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
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:
PYTHONPATH=/home/(user)/webapps/django/:/home/(user)/lib/python2.5 DJANGO_SETTINGS_MODULE=(site).settings
Offline
Nope. That didn't seem to do it.
Offline
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
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
Doh! It sure helps to actually point it to the right script!
Offline
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
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
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.
Offline