V
V
VMesser2019-12-18 00:21:33
Django
VMesser, 2019-12-18 00:21:33

Where are UBUNTU environment variables stored?

There is an application on Django, it takes the value os.environ['DB_HOST'], as I understand it, the OS environment variable.
I can't figure out where the value of this variable is stored. The env command in bash doesn't output DB_HOST, nor does it in global-settings.py.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2019-12-18
@leahch

Environment variables are stored in process memory, in your case it is bash - the command interpreter, which is in the terminal or in the console.
To set an environment variable, use the export command.
A small note, the variable is set ONLY in the current interpreter, and if you start a second terminal, then it will not be there anymore!
To set a variable for all interpreters, use the .bash_profile file in the user's root folder.
But! If you run, for example, from systemd as a system service, then you need to transfer variables separately!
In general, environment variables are stored in memory, and how they get there depends on the calling program or service.
In a particular case, it's easiest to write a script file to run and set environment variables like start.sh (just make it executable

#!/bin/bash

export MYENV=myvalue
python myscript.py

chmod +x start.sh
./start.sh

O
OnYourLips, 2019-12-18
@OnYourLips

Environment variables are passed from parent process to child.
Run your application by explicitly setting environment variables.
So:

export DB_HOST=foo
./server.py

Or like this:
DB_HOST=foo ./server.py

K
Karpion, 2019-12-18
@Karpion

Actually, the concept of "environment variables" is very ambiguous. For example, there are environment variables in every process - as you were told, during fork and exec they are copied from parent to child, then both can change them. Shells have two instances of such variables: one for inheritance, the other for internal use.
And there are environment variables in the kernel. This set is one for the entire system (although it can be its own in virtual machines).
Try googling "os.environ" - it's pretty well chewed up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question