Try to kill a spawn process with shell true and detached true nodejs
Asked Answered
C

1

6

I am trying to write a simple node.js application in order to run selenium in a dedicate shell and then close the shell and the corresponding child process (at the moment after a timeout of 5 seconds in order to test it). Here the code I have so far:

const spawn = require('child_process').spawn;
const seleniumHub = spawn('java', ['-jar', 'selenium-server-standalone-2.48.2.jar'], { shell: true , detached: true });

seleniumHub.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

seleniumHub.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
});

seleniumHub.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
});

setTimeout(function () {
    // process.kill(seleniumHub.pid); no errors but shell not closed
    // seleniumHub.kill('SIGINT'); completely ignored 
}, 5000);

Selenium correctly runs, but I am not able to close the opened shell. If I try to close it through its pid, I get an error that the pid doesn't exist. If I try to call the kill method on the child process, it completely ignores the command.Any suggestion please?

Thanks

p.s. I am on a Windows machine

Cantaloupe answered 16/5, 2017 at 17:20 Comment(0)
C
5

OK I found the solution myself and I am posting it here if it can be useful for someone. As workaround I used again spawn and called the taskkill of Windows, passing f and t as parameters:

spawn("taskkill", ["/pid", seleniumHub.pid, '/f', '/t']);
Cantaloupe answered 16/5, 2017 at 17:45 Comment(1)
I used this library for cross platform support. npm.im/tree-killSkeet

© 2022 - 2024 — McMap. All rights reserved.