简体   繁体   English

如何杀死命令行进程从 child_process nodejs 开始

[英]how to kill command line process start with child_process nodejs

how can i stop it, it keep running after i stop nodejs.我该如何停止它,它在我停止 nodejs 后继续运行。 I try process.kill() but not working我尝试 process.kill() 但不工作

const process = require('child_process')
const child = process.exec('ping 8.8.8.8 -t')
setTimeout(()=>child.kill(2),10000)

after a few hour reading nodejs docs, i found this for who have same my problem几个小时阅读 nodejs 文档后,我发现这个问题与我的问题相同

const process = require('child_process')
const child = process.execFile('ping 8.8.8.8',['-t'],{cwd:'your working direct'})
setTimeout(()=>child.kill(2),10000)//now kill() working

I modified your code as below to flush out the error:我修改了您的代码如下以清除错误:

const process = require('child_process')
const child = process.spawn('ping 8.8.8.8 -t') // instead of process.exec
setTimeout(() => {
    console.log(child.kill(2)) // --> false == failed
}, 10000);

child.on('error', (code, signal) => {
    console.log(`${code}`) // --> Error: spawn ping 8.8.8.8 -t ENOENT
});

The ping command requires an argument after the -t ( as per terminal: ping: option requires an argument -- t ping 命令在 -t 之后需要一个参数(根据终端: ping: option requires an argument -- t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM