Created a childprocess using shelljs
!/usr/bin/env node
require('/usr/local/lib/node_modules/shelljs/global');
fs = require("fs");
var child=exec("sudo mongod &",{async:true,silent:true});
function on_exit(){
console.log('Process Exit');
child.kill("SIGINT");
process.exit(0)
}
process.on('SIGINT',on_exit);
process.on('exit',on_exit);
Child process is still running.. after kill the parent process
If you can use node's built in child_process.spawn
, you're able to send a SIGINT
signal to the child process:
var proc = require('child_process').spawn('mongod');
proc.kill('SIGINT');
An upside to this is that the main process should hang around until all of the child processes have terminated.
// only windows
const module_child_process = require('child_process');
let proc = module_child_process.spawn("cmd");
let pid = proc.pid;
let cmd = `wmic process where parentprocessid=${pid} get ProcessId`;
let out = module_child_process.execSync(cmd);
let stdout = out.toString();
let pids=[];
let lines = stdout.split("\n");
for (let i = 0; i < lines.length; i++)
{
try
{
let data = lines[i].toString();
let val = parseInt(data.trim());
if (!isNaN(val))
{
//console.log(val.toString());
pids.push(val);
}
}
catch (ex)
{
}
}
for (let i = 0; i < pids.length; i++)
{
module_process.kill(pids[i]);
/* or
let cmd = `taskkill /pid ${pids[i].toString()} /T /F`;
console.log(cmd);
module_child_process.execSync(cmd);
*/
}
proc.kill();
proc = null;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.