(x-posted from StackOverflow)

I have set up cron to execute the following command for django-cron (django 1.10.5, python 3.5, anaconda 4.2.0) in ubuntu 16.04:

* * * * * ./manage.py runcrons "app.crons.RunCronJob"

Here is my file tree, which is located directly under the highest level of the remote site (/):

|-- user
|   |-- project
|   |   |-- projectname
|   |   |   |-- website
|   |   |   |   |-- file.txt
|   |   |   |   |-- manage.py
|   |   |   |   |-- app (a django app)
|   |   |   |   |   |-- crons.py

RunCronJob is a cronjob within crons.py that appends a line to file.txt under /website. When I enter the /website directory and manually do ./manage.py runcrons "app.crons.RunCronJob", it works just fine. However, when the cronjob I have set up is run, it does not append anything to my text file. I have verified that the cron task is running every minute with both grep cron /var/log/syslog a mailing service, but the data I receive doesn't seem to tell me anything useful about why it isn't working.

(Note for python: I have added os.chdir(os.path.dirname(sys.argv[0])) to the top of my crons.py to ensure that the text file gets written to in the correct directory, no matter where the command is run from. E.g., running website/manage.py runcrons "app.crons.RunCronJob" from /projectname is working for me as well. For this reason, I don't suspect my issue is related to django, the django-cron library or python.)

I have viewed some other answers which discuss some common problems with cron and have tried my best to fix any problems: my crontab has a newline at the end, I have set my timezone (though this should not affect my cronjob), pgrep cron returns a number, I have set the permissions for my text file, I have restarted cron many times, etc.

Although I have also tried to properly set my environment paths, I suspect that some part of it is still wrong. I tried various things like adding /home in front of /user, removing the dot and/or slash in front of manage.py, and adding the absolute filepath in front of my command, all to no avail.

Even though I think my suspect is in how I am setting my paths, the error could still be somewhere else in my project. What issue am I overlooking, or how should I diagnose it?

1 Answer 1


I have found the answer to my problem, and in doing so also realized that my title for this post is in fact incorrect: crontab was executing, but the issue was that the python commands within my cron script were not being executed.

The reason for this was that crontab was executing with a different $PATH variable than when I ran my commands directly in the console, described exactly as in the accepted other answers I linked. My mistake was that I completely misunderstood the $PATH variable; I thought it was supposed to point to the executable in my project, whereas in actuality it contains the paths to folders that interpret the scripts (like the python interpreter). So my scripts were not being executed because cron could not find an interpreter to execute them. A very silly mistake which could have been avoided by reading the documentation!

I had absolutely no issues with django, django-cron or python.

Summary: cron used different path. Added the default environment path (type $PATH in console to view).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.