How to Change the Environment Variables of Another Process in Unix

Is there a way to change the environment variables of another process in Unix?

Via gdb:

(gdb) attach process_id

(gdb) call putenv ("env_var_name=env_var_value")

(gdb) detach

This is quite a nasty hack and should only be done in the context of a debugging scenario, of course.

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.

Change environment variable value during execution

You can change the value during runtime - from inside the ruby script - using:

ENV['VARIABLE_NAME'] = 'value'

There is no option to change environment values from outside the process after it has been started. That's by design, as the environment will be passed at process startup.

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.

How can I set a global variable of a Linux process from another process?

You can use mmap to share a page via a file handler (shm_open) then communicate between two applications.

Is it possible to change the Environment of a parent process in Python?

No process can change its parent process (or any other existing process' environment).

You can, however, create a new environment by creating a new interactive shell with the modified environment.

You have to spawn a new copy of the shell that uses the upgraded environment and has access to the existing stdin, stdout and stderr, and does its reinitialization dance.

You need to do something like use subprocess.Popen to run /bin/bash -i.

So the original shell runs Python, which runs a new shell. Yes, you have a lot of processes running. No it's not too bad because the original shell and Python aren't really doing anything except waiting for the subshell to finish so they can exit cleanly, also.

Can a shell script set environment variables of the calling shell?

Your shell process has a copy of the parent's environment and no access to the parent process's environment whatsoever. When your shell process terminates any changes you've made to its environment are lost. Sourcing a script file is the most commonly used method for configuring a shell environment, you may just want to bite the bullet and maintain one for each of the two flavors of shell.



Related Topics



Leave a reply



Submit