Debian – Issue in Setting Environment Variable for Supervisor

debianenvironment-variableslinuxsudosupervisord

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?

Best Answer

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.

Related Topic