96

I really don't know how to get supervisor to work with environment variables.

Below is a configuration snippet.

[program:htNotificationService]
priority=2
#autostart=true
#autorestart=true
directory=/home/ubuntu/workspace/htFrontEnd/heythat/htsite
command = /usr/bin/python htNotificationService.py -service
stdout_logfile=/var/log/heythat/htNotificationService.log
redirect_stderr=true
environment=PATH=/home/ubuntu/workspace/htFrontEnd/heythat
stopsignal=QUIT

I have tried the following:

environment=PATH=/home/ubuntu/workspace/htFrontEnd/heythat
environment=PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat
environment=PATH=/home/ubuntu/workspace/htFrontEnd/heythat,PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat

When I start supervisor I get

htNotificationService: ERROR (abnormal termination)

I can start from the shell by setting the python path, but not from supervisor. In the logs I get an error that says that an import can't be found. Well, that would be solved if supervisor would work. I even have the path in /etc/environments?

Why will supervisor not work?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tampa
  • 75,446
  • 119
  • 278
  • 425

6 Answers6

134

Referencing existing env vars is done with %(ENV_VARNAME)s

See: https://github.com/Supervisor/supervisor/blob/master/supervisor/skel/sample.conf

Setting multiple environment variables is done by separating them with commas

See: http://supervisord.org/subprocess.html#subprocess-environment

Try:

environment=PYTHONPATH=/opt/mypypath:%(ENV_PYTHONPATH)s,PATH=/opt/mypath:%(ENV_PATH)s
alper
  • 2,919
  • 9
  • 53
  • 102
edibleEnergy
  • 1,739
  • 1
  • 13
  • 15
40

In your .conf file under the supervisord block, you can add all the environment key=value pairs as such

[supervisord]
environment=CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//",FLASK_CONFIG="TESTING"

[program:celeryd]
command=celery worker -A celery --loglevel=info -P gevent -c 1000

If you dont want to hardcode the variables but want to pull it in from the os environment, step 1 on your bash

Export env var

>> sudo export CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//"

Reload Bash

>> . ~/.bashrc

Check if env vars are set properly

>> env

Now modify the conf file to read - Note: prepend your env variables with ENV_

[supervisord]
environment=CELERY_BROKER_URL="%(ENV_CELERY_BROKER_URL)s",FLASK_CONFIG="%(ENV_FLASK_CONFIG)s"

[program:celeryd]
command=celery worker -A celery --loglevel=info -P gevent -c 1000
Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
  • 5
    great answer. these little variations are the important ones. – George Silva Apr 18 '17 at 21:44
  • Bear in mind that relying on environment variables only works if they were set when the `supervisord` process was started. – villasv Aug 15 '17 at 02:18
  • 8
    it can't work error: , : file: /usr/lib/python2.7/xmlrpclib.py line: 800 – wyx Jan 12 '18 at 10:19
  • Same here, any solutions to the expanded issue above? – Simon Melouah Jan 21 '19 at 22:17
  • @jetpackdata.com My configuration file `[supervisord] environment=CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//",FLASK_CONFIG="TESTING" [program:test_process] command=python -u test.py directory=/home/dinesh stdout_logfile=/home/dinesh/test_process_output.txt redirect_stderr=true` and python file is `import time count = 0 while True: count = count + 1 print(str(count) + ". This prints once every 2secs.%(ENV_FLASK_CONFIG)s") time.sleep(2)` . variable is not being replaced in my file. please help – dinu0101 Jun 24 '19 at 17:50
  • Can you explain what you're trying to do with `sudo export CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//"` that seems like it'd error or be a no-op. – edibleEnergy Nov 30 '21 at 18:14
29

this works for me. note the tabs before each line:

environment=
    CLOUD_INSTANCE_NAME=media-server-xx-xx-xx-xx,
    CLOUD_APPLICATION=media-server,
    CLOUD_APP_COMPONENT=none,
    CLOUD_ZONE=a,
    CLOUD_REGION=b,
    CLOUD_PRIVATE_IP=none,
    CLOUD_PUBLIC_IP=xx.xx.xx.xx,
    CLOUD_PUBLIC_IPV6=xx.xx.xx.xx.xx.xx,
    CLOUD_PROVIDER=c
FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
  • Can the same environment names be used for another app without colliding the values? –  Feb 23 '20 at 17:37
26

I know this is old but I just struggled with this for hours and wanted to maybe help out the next guy.

Don't forget to reload your config files after making updates

supervisorctl reread
supervisorctl update
twiclo
  • 589
  • 6
  • 10
7

If you install supervisor from a package installer, check which Supervisor version you are using. As of August 2016 you will get 3.0b2. If this is the case you will need a newer version of supervisor. You can get it by installing supervisor manually or by using Python's pip. Make sure all the dependencies are met, along with the upstart setup so that supervisord works as a service and starts on system boot.

gogasca
  • 9,283
  • 6
  • 80
  • 125
0

Sometimes, I find shell scripts more convenient to set up the environment.

So instead of this

[program:htNotificationService]
priority=2
directory=/home/ubuntu/workspace/htFrontEnd/heythat/htsite
command = /usr/bin/python htNotificationService.py -service
stdout_logfile=/var/log/heythat/htNotificationService.log
redirect_stderr=true
environment=PATH=/home/ubuntu/workspace/htFrontEnd/heythat
stopsignal=QUIT

I might have this in supervisor

[program:htNotificationService]
priority=2
directory=/home/ubuntu/runscripts/heythat/htsite
command = htNotificationService.sh 
stdout_logfile=/var/log/heythat/htNotificationService.log
redirect_stderr=true
stopsignal=QUIT

And htNotification.sh

#!/usr/bin/env bash

# change to directory
cd /home/ubuntu/workspace/htFrontEnd/heythat/htsite

# export environment variables here   
set -a
PATH=/home/ubuntu/workspace/htFrontEnd/heythat
USER=joe       # if it's needed
HOME=/home/joe # if it's needed
...
set +a

# or alternatively, source a separate environment file
source /home/ubuntu/.envs/htFrontEnd/heythat/htsite/development

# run the program
/usr/bin/python htNotificationService.py -service
Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79