Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use environment variables in supervisord commands

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)

like image 686
daveoncode Avatar asked Mar 07 '14 11:03

daveoncode


People also ask

What user does supervisord run as?

supervisor is definitely designed to run as root, but there's no real problem in running it as a non-root user. To do so, you have to make sure that the control socket and all of the log files can be created and written to by the executing user.

How does Supervisorctl work?

Supervisorctl allows a very limited form of access to the machine, essentially allowing users to see process status and control supervisord-controlled subprocesses by emitting “stop”, “start”, and “restart” commands from a simple shell or web UI.

What is the purpose of supervisord?

Supervisord or Supervisor daemon is an open source process management system. In a nutshell: if a process crashes for any reason, Supervisor restarts it. From the Supervisord website: Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.


2 Answers

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.

like image 169
robbyt Avatar answered Sep 28 '22 07:09

robbyt


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.

like image 38
colinhoernig Avatar answered Sep 28 '22 05:09

colinhoernig