Mic*_*tra 131 command exec node.js
是否可以从node.js中执行外部程序?是否有相当于Python os.system()或任何添加此功能的库?
Mar*_*ahn 129
var exec = require('child_process').exec;
exec('pwd', function callback(error, stdout, stderr){
// result
});
Run Code Online (Sandbox Code Playgroud)
MKK*_*MKK 70
exec的缓冲区大小为512k的内存限制.在这种情况下,最好使用spawn.使用spawn,可以在运行时访问stdout执行的命令
var spawn = require('child_process').spawn;
var prc = spawn('java', ['-jar', '-Xmx512M', '-Dfile.encoding=utf8', 'script/importlistings.jar']);
//noinspection JSUnresolvedFunction
prc.stdout.setEncoding('utf8');
prc.stdout.on('data', function (data) {
var str = data.toString()
var lines = str.split(/(\r?\n)/g);
console.log(lines.join(""));
});
prc.on('close', function (code) {
console.log('process exit code ' + code);
});
Run Code Online (Sandbox Code Playgroud)
zag*_*art 15
最简单的方法是:
const {exec} = require("child_process")
exec('yourApp').unref()
Run Code Online (Sandbox Code Playgroud)
UNREF要结束你的程序,而无需等待"yourApp"
这是exec 文档
来自 Node.js 文档:
Node 通过 ChildProcess 类提供了一个三向的 popen(3) 工具。
请参阅http://nodejs.org/docs/v0.4.6/api/child_processes.html
| 归档时间: |
|
| 查看次数: |
112407 次 |
| 最近记录: |