'use strict';
var spawn = require('child_process').spawn;
/**
* normalize exec args
* @param command
* @param options
* @returns {{cmd: *, shell: *, args: *, options: *}}
*/
function normalizeExecArgs(command, options){
var shell, args;
// Make a shallow copy before patching so we don't clobber the user's
// options object.
options = options || {};
if (process.platform === 'win32') {
shell = process.env.comspec || 'cmd.exe';
args = ['/s', '/c', '"' + command + '"'];
options.windowsVerbatimArguments = true;
} else {
shell = '/bin/sh';
args = ['-c', command];
}
if (options.shell) {
shell = options.shell;
}
return {
shell: shell,
args: args,
options: options
};
}
function exec(){
var parsed = normalizeExecArgs.apply(null, arguments);
return spawn(parsed.shell, parsed.args, parsed.options);
}
var thread = exec('ping www.baidu.com -t', {
env: process.env,
cwd: process.cwd()
});
thread.stdout.on('data', function (data){
console.log(data.toString());
});
thread.stderr.on('data', function (error){
console.log(error.toString());
});
thread.on('close', function (signal){
console.log(signal.toString());
});
setTimeout(function (){
thread.kill();
// process.kill(thread.pid);
// process.exit();
}, 6000);
代码跑起来后thread.kill并不能杀死后台ping进程,结果会一直打印,用process.kill和 process.exit也无效,任务管理器
中ping进程还在跑,本来想用electron做个命令管理工具 ,然后现在碰到这个问题计划泡汤,无法结束像ping www.baidu.com -t这样在后台一直跑的命令,前天做测试,没注意到这个问题,结果后台默默的开了100多个ping有木有啊 - -!