Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose I have a continuous process running that reads an environment variable:

export APPLE=abc123
watch echo \$APPLE

Now, is there any way to change the value that's being read during execution? I've tried suspending the process (ctrl-z), exporting a new value for the variable, then resuming (fg), but the old value is still displayed.

Is this possible? I'd prefer an answer that works with basic command-line tools and doesn't require specialized software, though requiring root privileges is OK.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

I've tried suspending the process (ctrl-z), exporting a new value for the variable, then resuming (fg), but the old value is still displayed.

This does not work because environment variables are inherited when the process starts – but when you use Ctrl-z and fg it doesn't restart watch; it just pauses and unpauses the old process.

The only way to change the environment for a running process, from the outside, is to do it from inside the process with a debugging tool. For example:

$ gdb -p $(pidof watch)
(gdb) p setenv("APPLE", "orange")
(gdb) q
Really detach? y
share|improve this answer
    
Exactly what I was looking for. Thank you! –  Coderer Oct 24 '12 at 14:48

Your Answer

 
discard

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.