2

I have a python script that connects to an external host, fetches some data, and populates a Django database with the data. The python script that populates the database uses these lines to setup the django environment:

path = os.path.normpath(os.path.join(os.getcwd(), '..'))
sys.path.append(path)
from django.core.management import setup_environ
import settings
setup_environ(settings)

Then I have a shell script that actually runs the python script:

export PYTHONPATH=$PYTHONPATH:/home/django/project_dir/
cd ~/project_dir/scripts/
~/virtualenv/bin/python my_script.py

And then my cron config, which is placed in /etc/cron.d/

0 1 * * * django ~/project_dir/scripts/my_script.sh > /var/log/django_cron.log

Note that the django project has it's own user and virtual environment.

The shell script runs fine when I am logged in as the django user. But the cron won't run! I get no errors in the log file. I'm sure it's something very simple, but I just don't see it...

3
  • Is anything showing up in /var/log/cron ? Commented Aug 10, 2010 at 0:33
  • Is there a reason not to use a managment command ? Commented Aug 10, 2010 at 7:31
  • I get entries in the cron log that the cronjob was run at the specified time, so no problem there. Commented Aug 10, 2010 at 7:59

2 Answers 2

2

The problem was actually that I have an environment variable to tell django if it is running in development or production. Crontab doesn't execute the .bashrc file where I export this variable so that had to be added to my shell script:

export FLAVOR=live
export PYTHONPATH=$PYTHONPATH:/home/django/project_dir/
cd ~/project_dir/scripts/
~/virtualenv/bin/python my_script.py
Sign up to request clarification or add additional context in comments.

Comments

0

Two things come to mind. I don't think cron will make the tilde substitution you expect -- perhaps I'm wrong, but try specifying the full path (i.e. /home/myuser/project_dir/...) in your cron entry as well as your scripts.

Also, what's the "django" in your cron entry? Is it the name of your script or is that a typo? Finally, to log possible errors from the execution of your script, try:

0 1 * * * /home/myuser/project/script.sh > /var/log/django_cron.log 2>&1

1 Comment

Actually, now I have changed the setup so I do 'crontab -e' as the django user, and include the line you demonstrated. That's probably the proper way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.