[英]How to kill a NodeJS child exec process?
I'm executing a Python program from NodeJS using exec
function of child_process
and I wish to kill the process on a button click.我正在使用 child_process 的exec
function 从NodeJS执行 Python 程序,我希望通过单击按钮来child_process
进程。 Am using Windows11.我正在使用 Windows11。
Here is my code:这是我的代码:
var process__ = undefined; // this is a global variable
function executePython(command_line_arguement){
const exec = require('child_process').exec;
process__=exec(`python program.py "${command_line_arguement}"`, { encoding: 'utf-8',detached : true }, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}, ${stderr}`);
}else{
console.log(stdout);
}
});
function onButtonClick(){
process__.kill('SIGINT');
}
However, this doesn't seem to halt or kill the python process that was triggered.但是,这似乎并没有停止或终止被触发的 python 进程。
Any guidance on how to proceed would be appreciable.任何关于如何进行的指导都将是可观的。
Thanks in advance !!提前致谢 !!
I believe windows needs a force kill to terminate the process ie something like我相信 windows 需要强制终止才能终止进程,例如
exec('taskkill /F /T /PID ' + process__.pid);
// F - force terminate
// T - kill all sub-processes
// PID - Process Id that you're targetting
See documentation for taskkill and the flags请参阅 taskkill 和标志的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.