Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How might it be possible to alter some variable in the env of an already running process, for example through /proc/PID/environ? That "file" is read-only.

Need to change or unset the DISPLAY variable of a long-running batch job without killing it.

share|improve this question
It's too late now, but for future reference, xpra could be interesting. – sr_ May 9 '12 at 9:56
xpra sounds useful. Normally I reroute to non-user displays hosted by Xvfb or Xephyr, but today I forgot and ran from cli rather than cron/at to troubleshoot output, so it's been annoying me at :0 – Marcos May 9 '12 at 11:12
up vote 15 down vote accepted

You can't do this without a nasty hacks - there's no API for this, no way to notify the process that its environment has changed (since that's not really possible anyway).
Even if you do manage to do that, there is no way to be sure that it will have any effect - the process could very well have cached the environment variable you're trying to poke (since nothing is supposed to be able to change it).

If you really do want to do this, and are prepared to pick up the pieces if things go wrong, you can use a debugger. See for instance this Stack Overflow question:
Is there a way to change another process's environment variables?


(gdb) attach process_id
(gdb) call putenv ("DISPLAY=your.new:value")
(gdb) detach

Other possible functions you could try to call are setenv or unsetenv.

Please do really keep in mind that this may not work, or have dire consequences if the process you target does "interesting" things with its environment block. Do test it out on non-critical processes first, but make sure these test processes mirror as close as possible the one you're trying to poke.

share|improve this answer
Yes I realize it's somewhat of a hack, risky and not guaranteed for reasons you mentioned. (Part of the reason I visit this group is for such non-conventional needs I can't seem to find ordinarily.) In this case setting DISPLAY to junk or empty merely resolves an annoyance and delay(unnecessary frequent screenshots over network,fine if they fail). Since child copies parent, I need only mod the parent env. Many new child &subchild processes are getting spawned and exit quickly in my batch job; those matter. I figured a debugger could do this, thanks--I could wrap that into a shell function. – Marcos May 9 '12 at 11:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.