Python virtualenv and crontab

I created a small simple bot in python to download the updated GeoNames daily updated files to be processed by the database.  These files are generated each day and remove the need to load a full 2GB dataset into the database to keep the database updated.

The python application I wrote utilized virtualenv so to ensure that the correct python version with the correct environmental settings was ran I needed to do a little extra

This process runs under my user on my home server so I added this line to my user’s crontab

crontab -e

the command that was added was

* 12 * * * cd /home/myuser/GeoNames && venv/bin/python pull.py > /dev/null 2>&1

This command will run every day at noon, local time

* 12 * * *

Change directory into the solution directory created

cd /home/myuser/GeoNames

the && this is used by the shell to run the following command ONLY if the proceeding one is successful.  So if for some reason the ‘cd’ command fails there will be no attept to run the python command.

 This will execute the pull.py script using the virtualenv’s version of python.  This is the relative path for the virtualenv python command to my project directory.

venv/bin/python pull.py

The ending redirect redirects the output of both the stderr and stdout to the /dev/null sink.

> /dev/null 2>&1

 

Got a lot of help from this thread:

 

Author: Axe
I am a senior developer. Ride my motorcycle whenever I can. Love photography, hiking, camping, cooking, coding, and cheese cake!

Leave a Reply