September 9, 2011

Combining python environments defined inside a virtualenv and a daemon process watched by supervisord posed a bit of a problem since environments defined by virtualenv need to be activated before they can be used using the "/bin/activate" shell script.

The best solution I found was inspired by this post resulted in this small shell script:

#!/bin/bash

VIRTUAL_ENV=$1
if [ -z $VIRTUAL_ENV ]; then
  echo "usage: $0 </path/to/virtualenv> <cmd>"
  exit 1
fi

. $VIRTUAL_ENV/bin/activate
shift 1
exec "$@"
deactivate

Make the virtualenv_cmd.sh script executable and add the following section to your supervisord configuration and your script will be running in your specified virtualenv and monitored by supervisord.

[program:virtualenv-app]
command = /path/to/virtualenv_cmd.sh /path/to/virtualenv python /path/to/script.py