Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling python script from crontab with activate [duplicate]

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

like image 602
Timmy Avatar asked May 27 '10 19:05

Timmy


People also ask

What is the use of crontab in Linux?

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.

How do I schedule a script to run on a crontab?

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:

How to show activity in crontab?

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.

Why can't I run a crontab test file?

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.


2 Answers

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
like image 161
mipadi Avatar answered Sep 22 '22 18:09

mipadi


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.

like image 30
Josir Avatar answered Sep 20 '22 18:09

Josir