16

How can I use an environment variable in a supervisord command? I tried:

flower --broker=$MYVAR

but it doesn't work (variable is not expanded), so I tried using an inline python script:

command=python -c "import os;os.system('flower --broker={0}'.format(os.environ['MYVAR']))"

The command above works, but then I'm unable to terminate the process using supervisorctl stop ...I get "stopped" back but the process is actually still running! How can I solve my issue? (I don't want to put that parameter inline)

daveoncode
  • 18,900
  • 15
  • 104
  • 159
  • Unless there is a special support builtin in supervisord; you need a shell or other process to expand an environment variable e.g., `command=sh -c 'flower --broker="$MYVAR"'` – jfs Mar 07 '14 at 14:01
  • mmm... why if I run "flower --broker=$MYVAR" in the shell it works?! – daveoncode Mar 07 '14 at 14:15
  • because *the shell* expands it – jfs Mar 07 '14 at 14:16
  • exactly... so, why is not expanded when supervisord runs my command? :P – daveoncode Mar 07 '14 at 14:19
  • 2
    you can run a command without spawning a shell, try `subprocess.call(["echo", "$PATH"])` vs. `subprocess.call("echo $PATH", shell=True)` – jfs Mar 07 '14 at 14:21

2 Answers2

19

According to the Supervisor docs, you can access environment variables in the command by prefixing ENV_ like: %(ENV_YOUR_VAR)s

http://supervisord.org/configuration.html#environment-variables

String expressions are evaluated against a dictionary containing the keys group_name, host_node_name, process_num, program_name, here (the directory of the supervisord config file), and all supervisord’s environment variables prefixed with ENV_.

However, according to this commit: https://github.com/Supervisor/supervisor/commit/2d6ca34582a8a07a5dd96ae45ef62cd58a459f4f this feature was added after version 3.2.

robbyt
  • 1,334
  • 14
  • 16
  • Keep in mind that the latest package in 14.04 is `3.0b2` but this requires `3.2` – Carson Ip Dec 01 '17 at 04:36
  • also keep in mind, that vars you specify with `environment=X="value"` are not available in the config file itself as `%(ENV_X)s`, but only for your command, as env var `$X`. – benzkji Mar 11 '21 at 15:07
5

I was able to use a system environment variable in a Supervisor command like this:

command=php artisan queue:listen --env=%(ENV_APP_ENVIRONMENT)s

The above command will expand to command=php artisan queue:listen --env=production if the APP_ENVIRONMENT environment variable is production.

Note: In the Supervisor config, you must prefix your system environment variables with ENV_, as specified in the documentation here.

colinhoernig
  • 942
  • 6
  • 9