Search code examples
tornadosupervisord

supervisorctl unable to reload virtual environment activate file


I am trying to run a tornado api with supervisor service.

This is the configuration I have written to run my application using supervisor:

[program:tornado_main]
command=/home/ubuntu/env/bin/python /home/ubuntu/<repo>/__main__.py
directory=/home/ubuntu/<repo>
user=ubuntu
stdout_logfile = /var/log/supervisor/tornado_main.log
stderr_logfile = /var/log/supervisor/tornado_main_err.log
environment=PATH=/home/ubuntu/env/bin,PYTHONPATH=/home/ubuntu/<repo>,VIRTUAL_ENV=/home/ubuntu/env/

This runs perfectly.

There are some portions in the code which depend on some environment variables which are written in the virtual environment's activate file.

Now, the issue is that supervisor is not able to read those environment variables and throws error. When I run it manually, it works fine.

What am I missing?


Solution

  • When you run env/bin/python, you are using the virtualenv without activating it. That's often enough, but if you really depend on the virtualenv being activated (e.g. because you want to launch subprocesses in that environment or use other environment variables), then you'll need to activate it in your supervisor config (and if you do this, you'll probably want to remove the environment line that duplicates what activate will do):

    command=bash -c 'source /home/ubuntu/env/bin/activate && python /home/ubuntu/<repo>/__main__.py'