从Node.js调用execl,execle,execlp,execv,execvP或execvp的方法 [英] A way to call execl, execle, execlp, execv, execvP or execvp from Node.js
问题描述
POSIX系统公开了exec
函数家族,该函数家族允许将可能不同的某些东西加载到当前进程中,并保持打开的文件描述符,进程标识符等.
POSIX systems expose family of exec
functions, that allow one to load something maybe different into current process, keeping open file descriptors, process identifier and so on.
可以出于多种原因完成此操作,在我的情况下,这是引导程序-我想更改自己进程的命令行选项,然后在现有进程上重新加载它,因此不会有子进程.
This can be done for variety of reasons, and in my case this is bootstrapping — I want to change command line options of my own process, and then reload it over existing process, so there would be no child process.
不幸的是,令我惊讶的是,我找不到在Node.js中调用任何exec*
函数的方法.那么,用其他映像替换当前正在运行的Node.js进程的正确方法是什么?
Unfortunately, to much of my surprise, I could not find the way to call any of exec*
functions in Node.js. So, what is the correct way to replace currently running Node.js process with other image?
推荐答案
我创建了一个模块来从NodeJS调用execvp函数: https://github.com/OrKoN/native-exec
I have created a module to invoke execvp function from NodeJS: https://github.com/OrKoN/native-exec
它是这样的:
var exec = require('native-exec');
exec('ls', {
newEnvKey: newEnvValue,
}, '-lsa'); // => the process is replaced with ls, which runs and exits
由于它是本机节点插件,因此需要安装C ++编译器.在Mac OS和Linux上的Docker中可以正常工作.可能在Windows上不起作用.经过节点6、7和8的测试.
Since it's a native node addon it requires a C++ compiler installed. Works fine in Docker, on Mac OS and Linux. Probably, does not work on Windows. Tested with node 6, 7 and 8.
这篇关于从Node.js调用execl,execle,execlp,execv,execvP或execvp的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!