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
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. ;)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With