0

Is there a way to inject a variable into a running process without a process listening for RPC requests?

For example if a process was running and using an environment variable, could I change that environment variable at runtime and make the process use the new value?

Are there alternative solutions for dynamically changing variables in a running process? Assume that this process is like a PHP process or a Javascript (node.js) process so I can change the source code... etc.

I think this is similar to passing state or communicating to another process, but I need a really lightweight way of doing so, without going over the network or using libraries or preferably not setting up an RPC server.

Solution does not have to be cross-platform. Prefer Linux.

CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
  • What programming language do you want the solution in? – Robert Harvey Jun 23 '14 at 05:00
  • Any, PHP, JS, Python, Ruby, Erlang, what ever you can think of. – CMCDragonkai Jun 23 '14 at 05:01
  • You don't have any idea what programming language this is going to be in? – Robert Harvey Jun 23 '14 at 05:02
  • It could be in any. I'm looking for a generic pattern that I can port and understand to any environment I'm working in. – CMCDragonkai Jun 23 '14 at 05:02
  • 1
    What do you mean by JavaScript process? – pmverma Jun 23 '14 at 05:03
  • Environment Variables are a Windows concept. Well, you have them in Linux too, but they're not cross-platform AFAIK. – Robert Harvey Jun 23 '14 at 05:03
  • @pmverma Node.js process obviously. Environment variables exist in Linux! I've used them before! – CMCDragonkai Jun 23 '14 at 05:04
  • We could do this dance all night, but maybe you ought to make your question a bit more specific. Tell us a bit more about the problem you're trying to solve. – Robert Harvey Jun 23 '14 at 05:04
  • I don't understand why you want to make this a specific programming language problem. If you must have a language, try PHP then. – CMCDragonkai Jun 23 '14 at 05:05
  • Because we're going to attempt to answer your question, and you're going to say "well, that's not quite what I meant," or "I've already tried that, but it didn't work," or "Well, that won't meet my need, because I need to frobnosticate the doomaflatchit at the same time." – Robert Harvey Jun 23 '14 at 05:06
  • How is that possible? I'm looking for any solution in any language. If it demonstrates that it resolves the problem of dynamically changing an environment variable in a lightweight manner while the process is running, then it's fine isn't it? – CMCDragonkai Jun 23 '14 at 05:07
  • Does it have to be cross-platform (still dancing)? – Robert Harvey Jun 23 '14 at 05:08
  • No. It can be on Linux. – CMCDragonkai Jun 23 '14 at 05:09
  • Based on [this question and it's answer](http://stackoverflow.com/questions/3416638/set-environment-variables-in-c), the answer to your question "Is there a way to inject a variable into a running process without a process listening for RPC requests?" appears to be "no." I believe that this is also generally true for Windows. – Robert Harvey Jun 23 '14 at 05:12
  • What about shared memory? http://stackoverflow.com/questions/14225010/fastest-technique-to-pass-messages-between-processes-on-linux – CMCDragonkai Jun 23 '14 at 05:15

2 Answers2

1

You can do it it java. Imagine this is your thread class:

public void ThreadClass extends Thread {
Boolean state;

ThreadClass(Boolean b) {
state = b;
    }

public void StopThread() {
state = false;
    }

public void run() {
while(state) { //Do whatever you want here}
    }
}

Now all you have to do is start this thread from your main class:

ThreadClass thread = new ThreadClass(true);
    thread.start();

And if you want to change the value of state, call the StopThread method in the thread like so:

try {
        thread.StopThread();
    } catch (InterruptedException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

This will change the state of the Boolean while the thread is running.

0

It appears that local IPC implementations like shared memory is the way to go: Fastest technique to pass messages between processes on Linux?

Community
  • 1
  • 1
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98