Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to run in crontab

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

with virtual env? I need to activate virtualenv first(Otherwise it does not work)

This is my virtual env:

source job/bin/activate
like image 836
user2054574 Avatar asked Feb 08 '13 15:02

user2054574


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 the env and if that was successful, excute the manage.py script. Since manage.py is supposed to have a python shebang and the virtual env sets the correct python interpreter, this should work.

Apparently cron usually runs with /bin/sh which does not know the source command. So one option is to use a dot as a source replacement. Another to set /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/ The article doesn't mention that source can be replaced by a ., but i've just tried it and it worked for me. So you have several options to choose from now, the article even has others. ;)

like image 93
Dirk Eschler Avatar answered Oct 27 '22 10:10

Dirk Eschler


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

like image 34
Igor Avatar answered Oct 27 '22 09:10

Igor