Env variable isn't set in my app on a server. But it exists

On a server I run Arch Linux. I have an env variable set:

 // in /etc/environment

 MY_VAR=aabbcc

I’ve reconnected to a server to reload the file. Then I connect to a node or Elixir app on that server:

> System.get_env "MY_VAR"

nil

I disconnect from a node and the variable is set, as it should:

  echo $MY_VAR
  aabbcc

Why?

How do you start your application?

Personally I have never used /etc/environment, I do either set the environment on a per user level in their respective environments or shells RC files or for services in the unit description.

Since I never used that file to set env vars, I’m not sure from what point on it will be available and if systemd controled services would “see” its content.

via systemd

/home/user123/my_app start

Actually, I even have the same var in ~/.profile

That one is definitively not read by systemd, even if you set the User directive accordingly.

The save way to inject environment variables into a systemd service unit is to use EnvironmentFile and Environment directives.

1 Like

but /etc/environment is read

I have googled a bit. And it seems as if /etc/environment is not read for services, but only for user sessions. You can set it as EnvironmentFile in the unit description, but I’d not do this for env vars that contain secrets, as every user will be able to see that one.

How do you set env. variable in your apps?

As I said already, I use the EnvironmentFile and Environment directives in the unit file.

3 Likes