[英]Kill (like ctrl+c) exec (child process) on nodejs
I have the following code in index.js:我在 index.js 中有以下代码:
var child = exec(
'node app.js --board=' + data.data.board + ' --link=' + data.data.link,
function (error, stdout, stderr) {
if (error) console.log('exec error: ' + error)
if (stderr) console.log('stderr: ' + stderr)
}
)
.stdout.on('data', function(log_data) {
console.log(log_data)
socket.emit('listener', { ty: 'user_p_logs', num: data.num, log: log_data })
fn(true)
})
This is the code for the run app.js
, but I cannot stop this child process on index.js
这是run app.js
的代码,但我无法在index.js
上停止这个子进程
I tried the following in index.js
:我在index.js
尝试了以下内容:
child.kill()
child.kill('SIGINT')
process.exit() // This stopped index.js, instead of its child.
update: any answers ?更新:有答案吗?
What you're assigning to child
is the return value of .stdout.on
, which is the stdout
event emitter, not the child
process.您分配给child
是.stdout.on
的返回值,它是stdout
事件发射器,而不是child
进程。 You need it to be the child process.您需要将其作为子进程。 Don't overdo the chaining.不要过度链接。 :-) :-)
var child = exec(
'node app.js --board=' + data.data.board + ' --link=' + data.data.link,
function (error, stdout, stderr) {
if (error) console.log('exec error: ' + error)
if (stderr) console.log('stderr: ' + stderr)
}
);
child.stdout.on('data', function(log_data) {
console.log(log_data)
socket.emit('listener', { ty: 'user_p_logs', num: data.num, log: log_data })
fn(true)
});
Then, child.kill
should work.然后, child.kill
应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.