3

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.

1
  • a more normal solution than gdb would be to poll the variable from a file or socket or such in your script Commented Nov 6, 2024 at 18:47

1 Answer 1

8

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 
1
  • I get this error: No symbol "setenv" in current context. On Mac Sierra Commented Dec 14, 2017 at 5:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.