How to use environment variables in supervisord commands
Asked Answered
S

2

17

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)

Suicidal answered 7/3, 2014 at 11:22 Comment(5)
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"'Schizophrenia
mmm... why if I run "flower --broker=$MYVAR" in the shell it works?!Suicidal
because the shell expands itSchizophrenia
exactly... so, why is not expanded when supervisord runs my command? :PSuicidal
you can run a command without spawning a shell, try subprocess.call(["echo", "$PATH"]) vs. subprocess.call("echo $PATH", shell=True)Schizophrenia
P
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.

Porterfield answered 11/6, 2015 at 14:51 Comment(2)
Keep in mind that the latest package in 14.04 is 3.0b2 but this requires 3.2Pondweed
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.Carlo
L
6

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.

Lemaster answered 6/11, 2015 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.