启动后分离一个 spawn 子进程
Detach a spawn child process after the start
我这样启动一个 spawn 子进程:
let process = spawn(apiPath, {
detached: true
})
process.unref()
process.stdout.on('data', data => { /* do something */ })
当我启动进程时,我需要保持它的附加状态,因为我想读取它的输出。但是就在关闭我的节点进程(父进程)之前,我想分离所有未完成的子进程以将它们 运行 保留在后台,但是正如 the documentation 所说:
When using the detached option to start a long-running process, the process will not stay running in the background after the parent exits unless it is provided with a stdio configuration that is not connected to the parent.
但是使用选项 stdio: 'ignore'
我无法阅读 stdout
,这是个问题。
我之前尝试过手动关闭管道来关闭父进程,但没有成功:
// Trigger just before the main process end
process.stdin.end()
process.stderr.unpipe()
process.stdout.unpipe()
经过多次测试,我至少找到了一种方法来解决这个问题:在离开主进程之前销毁所有管道。
一个棘手的问题是子进程必须正确处理管道破坏,否则它可能会出错并关闭。在这个例子中,节点子进程似乎没有问题,但它可能与其他场景不同。
main.js
const { spawn } = require('child_process')
console.log('Start Main')
let child = spawn('node', ['child.js'], { detached: true })
child.unref() // With this the main process end after fully disconnect the child
child.stdout.on('data', data => {
console.log(`Got data : ${data}`)
})
// In real case should be triggered just before the end of the main process
setTimeout(() => {
console.log('Disconnect the child')
child.stderr.unpipe()
child.stderr.destroy()
child.stdout.unpipe()
child.stdout.destroy()
child.stdin.end()
child.stdin.destroy()
}, 5000)
child.js
console.log('Start Child')
setInterval(function() {
process.stdout.write('hello from child')
}, 1000)
产出
Start Main
Got data : Start Child
Got data : hello from child
Got data : hello from child
Got data : hello from child
Got data : hello from child
Disconnect the child
我这样启动一个 spawn 子进程:
let process = spawn(apiPath, {
detached: true
})
process.unref()
process.stdout.on('data', data => { /* do something */ })
当我启动进程时,我需要保持它的附加状态,因为我想读取它的输出。但是就在关闭我的节点进程(父进程)之前,我想分离所有未完成的子进程以将它们 运行 保留在后台,但是正如 the documentation 所说:
When using the detached option to start a long-running process, the process will not stay running in the background after the parent exits unless it is provided with a stdio configuration that is not connected to the parent.
但是使用选项 stdio: 'ignore'
我无法阅读 stdout
,这是个问题。
我之前尝试过手动关闭管道来关闭父进程,但没有成功:
// Trigger just before the main process end
process.stdin.end()
process.stderr.unpipe()
process.stdout.unpipe()
经过多次测试,我至少找到了一种方法来解决这个问题:在离开主进程之前销毁所有管道。
一个棘手的问题是子进程必须正确处理管道破坏,否则它可能会出错并关闭。在这个例子中,节点子进程似乎没有问题,但它可能与其他场景不同。
main.js
const { spawn } = require('child_process')
console.log('Start Main')
let child = spawn('node', ['child.js'], { detached: true })
child.unref() // With this the main process end after fully disconnect the child
child.stdout.on('data', data => {
console.log(`Got data : ${data}`)
})
// In real case should be triggered just before the end of the main process
setTimeout(() => {
console.log('Disconnect the child')
child.stderr.unpipe()
child.stderr.destroy()
child.stdout.unpipe()
child.stdout.destroy()
child.stdin.end()
child.stdin.destroy()
}, 5000)
child.js
console.log('Start Child')
setInterval(function() {
process.stdout.write('hello from child')
}, 1000)
产出
Start Main
Got data : Start ChildGot data : hello from child
Got data : hello from child
Got data : hello from child
Got data : hello from child
Disconnect the child