how do i call a python script from crontab that requires using activate (source env/bin/active)?
And crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. First, let’s create a simple script that we will be scheduled to run every 2 minutes.
To schedule our script to be executed, we need to enter the crontab scheduling expression into the crontab file. To do that, simply enter the following in the terminal: You might be prompted to select an editor, choose nano and append the following line to the end of the opened crontab file:
Here is the script. Show activity on this post. Just use crontab -e and follow the tutorial here. Look at point 3 for a guide on how to specify the frequency. Show activity on this post. and use the full path of your foo.py file in your crontab. See documentation of execve (2) which is handling the shebang.
The CronTab can be confusing, but there are an abundance of guides out there to help. Depending on how you edited your Crontab, this could be a permission issue. Using bash, I always did the following: This was able to successfully run and output the test file as required.
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 not 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/user/virtualenv/python /home/user/bin/cronjob
My approach is always to keep crontab as simple as possible and treat all configurations inside scripts called by crontab.
1) Create a shell script: for example /var/webapp/cron.sh
#!/bin/sh
PATH="/var/webapp/.env/bin:$PATH"
export PATH
cd /var/webapp/
python test.py
where /var/webapp/.env/bin is the virtualenv location. Setting PATH, you don't need to run source ../activate
2) Set properly your environment. For example, for a Django application:
#!/usr/bin/env python
import os
from datetime import datetime
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.production'
os.environ['DJANGO_CONF'] = 'settings.production'
from util.models import Schedule
dset = Schedule.objects.all()
for rec in dset:
print rec
print 'cron executed %s' % datetime.today()
On this example, django settings are located on settings/production.py
3) Finally, edit /etc/crontab. For example, to be execute each half hour, every day:
1,31 * * * * root /var/webapp/cron.sh >> /var/webapp/cron.log
Notice that it's important to generate logs to help you find errors or debug messages.
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