views:

179

answers:

1

how do i call a python script from crontab that requires using activate (source env/bin/active)?

+2  A: 

virtualenv's activate script is pretty simple. It mostly sets the path to your virtualenv's Python interpreter; the other stuff that it does (setting PS1, saving old variables, etc.) aren't really necessary if you're in an interactive shell. So the easiest way is just to launch your Python script with the correct Python interpreter, which can be done in one of two ways:

1. Set up your Python script to use your virtualenv's Python interpreter

Assuming your virtualenv's interpreter is at ~/virtualenv/bin/python, you can put that path at the top of your Python script:

#!/home/user/virtualenv/bin/python

And then launch your script from your crontab, as normal.

2. Launch the script with the proper Python interpreter in your cronjob

Assuming your script is at ~/bin/cronjob and your virtualenv's Python interpreter is at ~/virtualenv/python, you could put this in your crontab:

* * * * * /home/virtualenv/python /home/bin/crontab
mipadi
what about the paths to easy_install libraries?
Timmy
If they're installed in the virtual environment as well, or the "global" site-packages directory, they should be found; otherwise, you will have to put them in your `$PYTHONPATH`, which gets a bit uglier but can be accomplished by launching the Python bin with `/usr/bin/env` or somesuch.
mipadi
the last code block should probably end with "/home/bin/cronjob"
Van Nguyen