How to run custom manage.py in crontab + virtual env?

How to run crontab

*/1 * * * * /home/user/Desktop/job/dp/ python manage.py statistics 

with virtual env? I need to activate virtualenv first (otherwise it won't work)

This is my virtual env:

 source job/bin/activate 
+4
source share
2 answers

EDITED

Try something like this:

 */1 * * * * . /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics 

This should be read as: activate env, and if it was successful, excute manage.py script. Since manage.py is supposed to have python shebang, and virtual env installs the correct python interpreter, this should work.

Obviously, cron usually works with /bin/sh , which does not know the source command. Thus, one option is to use a dot as a replacement for source . Another install /bin/bash in the cron file:

 SHELL=/bin/bash */1 * * * * source /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics 

Read more about this issue at: http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/ This article does not mention that source can be replaced with . but I just tried and it worked for me. Thus, you have several options, the article even has others.;)

+13
source

Use something like ~/envs/someenv/lib/python /path/to/your/script

In your situation, it will look like

*/1 * * * * ~/envs/someenv/lib/python /home/user/Desktop/job/dp/manage.py statistics

+4
source

All Articles