3

In NodeJS, how does one make the c-level exec call, like other languages and runtimes allow (e.g. Python's os.exec() or even Bash's exec), please?

Unlike the child_process module (and all the wrappers I've found for it so far), this would be meant to completely replace the current NodeJS process by the execed process.

What I am aiming for is to create a NodeJS cli adaptor of sorts, that you provide with one set of configuration options and you can use it to dispatch different tools that use the same configuration, but each has a different expectations on the format (some expect ENV VARs, some command-line arguments, some need a file, ...).

The reason why I am looking for such an exec call is that, once the tool gets dispatched, I have no expectation on the NodeJS process sticking around -- there's no reason for it to continue existing. At the same time, I need to tool to become the foreground process (accept all signals, control characters, ...). It seems to me that it would be possible to forward all such events from the parent NodeJS to the child tool (if I used child_process), but it seems a bit too much to implement from scratch...

Cheers!

2
  • If you use child_process.spawn() and set the detached option, you can then just exit your node.js process and the new process will be left running on it's own. Read the doc here. To get your node.js process to actually exit, you can either call process.exit() or do a subprocess.unref() on the process identifier for the new child process you started. Commented Mar 29, 2020 at 11:13
  • Thanks for responding. I have noticed the detached option, but did not notice that expanded section about it in the docs. Testing this a little, it does seem like it is possible to start up the child and have the parent die, but that is still more like daemonization. I specifically need to child to be in foreground and for the user to be able to interact with it. I have only been able to make the child somewhat linger around, but it acts like it is only half-way connected to the terminal... Would you mind giving an example where you start, say, a new bash session, please? Commented Mar 29, 2020 at 20:02

1 Answer 1

2

OK, so I think I have finally got it. I am posting this as an answer as it actually shows a real code example:

child_process.spawnSync(
  "bash",
  [
    "-ic",
    ["psql"],
  ],
  {
    stdio: "inherit",
  }
);

process.exit();

This will spin up bash (with control tty) and inside it psql. Pressing ctrl-c does not kill the node process; it is, as expected, sent to psql. I have no idea if it would be possible to rewrite this code in a way where the node process terminates before psql and bash, but for my use case, this does it. Maybe it would be even possible to get it done without bash, but I think that bash -i is actually the key here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.