Set Environment Variables for a Process

Setting Environment Variables for Node to retrieve

Environment variables (in this case) are being used to pass credentials to your application. USER_ID and USER_KEY can both be accessed from process.env.USER_ID and process.env.USER_KEY respectively. You don't need to edit them, just access their contents.

It looks like they are simply giving you the choice between loading your USER_ID and USER_KEY from either process.env or some specificed file on disk.

Now, the magic happens when you run the application.

USER_ID=239482 USER_KEY=foobar node app.js

That will pass the user id 239482 and the user key as foobar. This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables.

Passing environment variables to a process

There are two ways to set the pass variables from your current shell a running program,

Either use the export built-in with syntax as

$ export MYVALUE=5
$ echo "MYVALUE is $MYVALUE"
MYVALUE is 5

This syntax allows the variable to take effect in current shell in all the subsequent sub-shells you are invoking( for command-substitution or process-substitution, etc) and the variable stays alive even after the sub-shells are terminated.

(or) as asked in the question, if you directly send it to the command as

$ MYVALUE=5 bash -c 'echo "MYVALUE is $MYVALUE"'
MYVALUE is 5

the value is passed only to the sub-shell(the one started with bash -c) and has no effect on parent shell once it exits. You can observe the MYVALUE from the above syntax now, it will be empty.

$ echo $MYVALUE
$

Hope this answers your question.

How do you set environment vars for a process?

You just set the environment variable of the Process to a [String: String] containing the variable to value mappings.

let process = Process()
// ...
process.environment = ["home": "/Users/foo"]

If you want to pass on the current environment, you can do it like so:

let process = Process()
// ...
let environment = ProcessInfo.processInfo.environment
environment["home"] = "/Users/foo" //optionally set new vars, or overwrite old ones
process.environment = environment

If you want to set the working directory, that's not dictated by a environment variable, but rather, via the currentDirectoryPath property.

let process = Process()
// ...
process.currentDirectoryPath = "/Users"

Changing environment variable of a running process

In general, you can only influence a process's environment variables at the time the process starts up. If you need to communicate a change to a running process, the environment isn't the right tool.

However, this question has some answers that suggest ways to overcome this limitation.

Edited to add in light of discussion in the question's comments: A fairly good way of communicating occasionally changing setup to a running process is to designate a configuration file where the LOGLEVEL value is set, send a SIGHUP to the process, and have the process reread the configuration file upon receipt of SIGHUP.

Set an environment variable from a process that will be visible by all processes

This is simply not possible.

Setting an environment variable (or changing your current environment) is only visible from the children (and descendants) processes of your current process.

Other processes, in particular the parent process (usually the shell running in the terminal where you start your program) are not affected.

You might play dirty tricks like e.g. adding lines into $HOME/.bashrc etc. But you should not.

You just need to document what environment variables are relevant. It is the user's responsibility to set environment variables (perhaps by manually editing his $HOME/.bashrc etc etc). Leave that freedom to your user. Explain to him how to do that and why.

You edited your question to explain that

I have 10 processes that use the same library. The problem is that in that library a checking procedure ( which is CPU hungry ) is performed. I want to avoid that library checking procedure to be executed for every process.

But you definitely should not need to change environment variable for that.

You could

  1. decide and document that the checking is not performed, unless some particular environment variable (or some program argument) is given

  2. decide that the checking is given a particular file name, and use file locked write to write that file, and use file locked reads to read it again

  3. Have the checking write its result in some known in advance file, and read that file before deciding it you want to make the costly checks

  4. Have one process starting all the others, and inform them about the check (perhaps indeed setting some environment variable or some program argument) or use some Inter Process Communication trick to communicate with the others (you could use sockets, locked files, shared memory, etc etc...)

  5. Do many other tricks.

Get environment variables of a different process

Within links posted I found Oleksiy Gapotchenko's blog. He developed a ready-to-use nuget package.

Reads environment variables of a process. The functionality is
achieved by reading the process environment block (PEB) at the
operating system level.

blog.gapotchenko/reading-environment-variables

github/Gapotchenko.FX.Diagnostics.Process

nuget/packages/Gapotchenko.FX.Diagnostics.Process

How to set child process' environment variable in Makefile

Make variables are not exported into the environment of processes make invokes... by default. However you can use make's export to force them to do so. Change:

test: NODE_ENV = test

to this:

test: export NODE_ENV = test

(assuming you have a sufficiently modern version of GNU make >= 3.77 ).



Related Topics



Leave a reply



Submit