How to use crontab

Post date: May 10, 2018 5:35:07 PM

If you want to setup a program to run on a specific time, crontab is your friend.

I found this article the best

http://www.adminschoice.com/crontab-quick-reference

As you may see that cron starts in a specific path and specific environment. This cannot be changed. And this is not ideal for you:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=kittipat.kampa>
X-Cron-Env: <USER=kittipat.kampa>
X-Cron-Env: <HOME=/Users/kittipat.kampa>

What if you want to schedule your python program in a certain environment?

Yes, you can do so. You will need to add a script to load your environment and go to the working dir. For example:

# activate your environment
cd /Users/kittipat.kampa/.virtualenvs/master
source bin/activate
# go to your working dir and run your script from there
cd /your/working/dir/
python your_python_program.py

references:

https://stackoverflow.com/questions/4150671/how-to-set-virtualenv-for-a-crontab/12848443