Skip to content

Instantly share code, notes, and snippets.

@trAve3113r
Last active August 18, 2021 09:46
Show Gist options
  • Save trAve3113r/fa98a8379af1486edc167e523967c15a to your computer and use it in GitHub Desktop.
Save trAve3113r/fa98a8379af1486edc167e523967c15a to your computer and use it in GitHub Desktop.
Run python scripts in virtualenv using cron
# execute a python script in virtualenn using cron
# web links to solutions
# http://www.adminschoice.com/crontab-quick-reference
# https://www.unix.com/man-page/linux/5/crontab/
PATH="" # RUN 'echo PATH' as root
[email protected]
# setup e-mail first :: https://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/
HOME=/home/www/production/mydjangoproject; /home/virtualenvs/mydjangoproject-venv/bin/python
1 * * * 1-5 $HOME/my_script.py #OR /ohlc/my_script.py
# OR
1 * * * 1-5 manage.py my_django_command
# https://serverfault.com/questions/901495/cron-job-running-a-django-python-command-through-a-virtual-environment-not-worki
* * * * * cd /home/www/production/mydjangoproject; /home/virtualenvs/mydjangoproject-venv/bin/python manage.py mydjangocommand
# https://monicagranbois.com/blog/python/cron-and-python-virtualenv/
* * * * * * cd /home/myuser/myproject && venv/bin/python bot.py config/config.ini > /dev/null 2>&1
# "2>&1" mean :: redirect error messages (stderr) to the visible command line (stdout) as opposed to sending error e-mail
# "> /dev/null " :: only captures standard error and sends e-mail
# use chronic to capture errors instead of MAILTO which e-mails ALL of a cronjob's output(spam!)
# apt-get install moreutils
* * * * * cronic cd /home/www/production/mydjangoproject; etc
# documentation :: https://habilis.net/cronic/
# --------------------------------------------------------------- #
# OR :: my_other_crontab_implementation
* * * * 1-5 some/command.sh
# then :: 'command.sh' does bin activate and executes a script ?
# --------------------------------------------------------------- #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment