0
votes

I have a script in my Django project that needs to run every 5 mins.

I am using this cron module to run my script named 'cron.py' present in myproject/myapp/cron.py

But when I am running the sever, the script is not running at all (no output) but the job is showing when I am doing "python manage.py crontab show" as mentioned in the link above.

Also, when I run the script by explicitly going and running by issuing 'python cron.py', it is showing (notifier is the name of myapp)

Traceback (most recent call last):

File "cron.py", line 2, in

from notifier.models import *

ImportError: No module named notifier.models

Everything is working perfectly when i'm copy-pasting this same code in cron.py on python command line and running.

Can you please help me out in the right direction? I've looked everywhere on the internet.

Using Python 2.7.10 and Django 1.8 with Postgresql DB.

1
You probably need to set the environmental variables on the user that executes the cron job, so that it can access all the Django files in your application. - wobbily_col

1 Answers

0
votes

Check you are in the correct directory, and you are possibly in the correct virtual environment.

At the top of your cron script you may want to:

import os
os.chdir(os.path.dirname(__file__))

or change to the root directory of the app.