UP | HOME

How to change the environment of a running (parent) process under Linux?

This is not actually intended and there is no clean way to do it.

However, it is possible to change an environment variable of an existing process using a debugger.

Specifically, this is about the debugger called gdb. This can attach itself to existing processes and carry out various operations. Among other things, changing environment variables.

To connect to a running process you must have gdb installed and know the respective process ID (PID). In the example below, the ID is 2811:

gdb --pid=2811

The debugger then reports with the GDB prompt:

(gdb) call putenv ("MYVAR=1234")

In the upper sample you can also find the command with which the environment variable of the process can be found. In this case, the variable MYVAR is set to the value 1234.

Please note that the respective process is stopped as soon as gdb is started with the parameter --pid. As soon as the variable has been set, the debugger can be ended again with the q and Enter keys - the process then runs again.

But the whole thing is just a dirty hack and should not be used in a productive environment.

-- 
Created on February 10, 2024 by Andreas Wittmann



Home - Sitemap - Privacy-policy/Impressum

Coypright 2024 by Andreas Wittmann
All rights reserved