const child_process = require("child_process");
const fs = require("fs");
let out = fs.openSync("./out.log", "a");
let err = fs.openSync("./out.log", "a");
let tinysvr = child_process.spawn("ejemplo", [], {
detached: true,
stdio: ['ignore', out, err]
});
tinysvr.unref();
console.log("bye");
detached参数的作用:
https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_detached
如果进程无法启动(可执行文件不存在, 路径错误etc), 用try-catch将这个spawn包起来也没有用.
只能通过:
process.on('uncaughtException', (e)=>{
console.error(`${e}`);
});
来捕获. (错误的spawn不会出现在调用栈上边. 这是一个比较可惜的地方.)