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
decide and document that the checking is not performed, unless some particular environment variable (or some program argument) is given
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
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
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...)
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
How to Use Sed to Extract Substring
What Happens If You Use the 32-Bit Int 0X80 Linux Abi in 64-Bit Code
How to Change the Number of Open Files Limit in Linux
Environment Variable Substitution in Sed
How to Preserve Quotes in Printing a Bash Script'S Arguments
Shell Script: Run Function from Script Over Ssh
Linux: Copy and Create Destination Dir If It Does Not Exist
How to Use Grep to Show Just Filenames on Linux
Apache Virtualhost 403 Forbidden
How to Shutdown a Spring Boot Application in a Correct Way
Sed In-Place Flag That Works Both on MAC (Bsd) and Linux
Using Printf in Assembly Leads to Empty Output When Piping, But Works on the Terminal
What Happens If There Is No Exit System Call in an Assembly Program
Forcing Bash to Expand Variables in a String Loaded from a File
How to Copy Commits from One Git Repo to Another
How to Obtain the Number of Cpus/Cores in Linux from the Command Line