问题
I created a simple python script that allows me to update a chosen animal to be displayed on the sites frontpage.
when I am in my ssh, I run it like this.
cd /www/site/mydirectory
python perform_daily_action.py
How do I run this in my crontab as a cronjob.
I tried to do
30 09 * * * cd /www/site/mydirectory;python perform_daily_action.py
Although this does not seem to work.
Suggestions?
回答1:
Try use this instead
30 09 * * * python /www/site/mydirectory/perform_daily_action.py
回答2:
The other answers are great, I would only suggest using full path also for Python
interpreter, because cron
environment might be different than the regular one and also at some point you might want to switch to virtualenv
and then you will definitely need Python
interpreter from virtualenv
folder.
回答3:
You must provide the full python location (for example /usr/bin/python) and/or perhaps give your machine some guidelines about your $PYTHONPATH. Try this:
30 09 * * * export PYTHONPATH=/www/site/mydirectory:.:$PYTHONPATH && /usr/bin/python /www/site/mydirectory/perform_daily_action.py
来源:https://stackoverflow.com/questions/5455425/django-python-how-should-this-cronjob-be-executed