0

I am using supervisorctl for running a program and environment variables to store the common variables.

For setting Environment Variable I am using /etc/environment. I have added the following as environment variable:

Foo=Bar 

Then I made sure the environment variable is set right by using the following command

echo $Foo > Bar 

This is a Sample Program Which I am trying to run using Supervisor:

echo $Foo while : do sleep 10s echo "I have completed" done 

When I run the program directly its working variable properly:

sh /home/data/trial.sh > Bar I have completed 

I have added Trial.conf in my /etc/supervisor/conf.d/ directory. This is my Trial.conf:

[program:Trial] command = sh /home/data/trial.sh 

When I run the program using supervisorctl

supervisor> start Trial Trial: started supervisor> fg Trial I have completed 

I found some solutions for setting the local environment variable for supervisorctl

I tried the following:

[supervisord] environment=Foo="%(ENV_Foo)s" 

But when I rebooted the supervisor and started the supervisor I got this error:

unix:///var/run/supervisor.sock no such file 

One behavior I noticed is that when I am using sudo Then I cannot access my environmental variables:

echo $Foo > Bar sudo echo $Foo > 

For that, I found a solution sudo -E su which will preserve $Foo for root.

Question: How can I make Supervisor access my local environmental variables?

1 Answer 1

0

You shouldn’t be trying to get supervisord to read your user’s environment variables. You should instead tell supervisord what to use for the environment variables in its config.

Environment variables won’t persist a reboot, they need to be set each time on boot or launching of a shell.

Keep in mind that it’s possible for users to see environment variables of other users so don’t store anything sensitive in them (like passwords). Use a properly protected config file.

8
  • I have some programs which aren't using supervisor to run them and I need to store all the variables at a common place. Environment variables at /etc/environment help me in achieving this. How can I access those environment variables when running a program using supervisor? Commented Mar 18, 2019 at 10:24
  • IIRC supervisord intentionally does not invoke a shell and thus won’t get environment variables. You must specify them in the supervisord config for that program. Again I really think you want to store these in a config file rather than env vars but if you’re dead set on env vars then you could setup a wrapper script that defines the env vars and then calls the program. Then you have supervisors call that wrapper script instead of the program. Commented Mar 18, 2019 at 10:31
  • Yes I have specified them above at /etc/supervisor/supervisord.conf using [supervisord] environment=Foo="%(ENV_Foo)s" But I am getting error Commented Mar 18, 2019 at 10:33
  • I saw that previously but you need to understand the order of operations here. Again, what you set for your local user’s environment variables won’t be accessible to supervisord. Think of this: 1) server boots, no env vars are present 2) startup scripts execute, one of them starts supervisord 3) supervisord reads it’s config, sees $Foo is empty and replaces %(ENV_Foo)s with an empty string, starts your program which now has Foo=<empty string> 4) you log in and set your local env var to something. Doesn’t matter to your program, it’s already running. Commented Mar 18, 2019 at 10:36
  • Yes, I see your point now. What should I use then to store common shared variables? Which will be available to the supervisor also and for the other programs which are outside the supervisor? Commented Mar 18, 2019 at 11:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.