[英]Kill detached child process in Node
I'm not fully understand why I can't kill a detached process.我不完全明白为什么我不能杀死一个分离的进程。 Can someone help me out?
有人可以帮我吗?
Server (child process)服务器(子进程)
const server = spawn(
'npm',
[
'run',
'watch:be',
],
{
detached: true,
},
);
Await for the server to up and running等待服务器启动并运行
await waitOn({
resources: [
`https://localhost:${process.env.SERVER_PORT}`,
],
delay: 1000,
timeout: 30000,
});
console.log('server is up and running');
Wait a couple more seconds再等几秒钟
await new Promise((resolve, reject): void => {
setTimeout((): void => {
resolve();
}, 2000);
});
console.log('Run test');
Kill the child server杀死子服务器
server.kill();
console.log('Shutdown server');
All of these are in the same file.所有这些都在同一个文件中。
The child process opened a new terminal window (when it does spawn
, which is expected), but doesn't close when kill
.子进程打开了一个新终端 window(当它执行
spawn
时,这是预期的),但在kill
时不会关闭。 Can someone point out what I have done wrong?有人可以指出我做错了什么吗?
server.kill();
As per the node.js documentation, The subprocess.kill()
method sends a signal to the child process.根据 node.js 文档,subprocess.kill
subprocess.kill()
方法向子进程发送信号。 When you use the detached
option, the node creates a separate process group for the child process and it is not part of the same process anymore.当您使用
detached
选项时,节点会为子进程创建一个单独的进程组,并且它不再是同一进程的一部分。
detached <boolean>: Prepare child to run independently of its parent process
That is the reason kill
is not appropriate to use when detached
is used.这就是
kill
在使用detached
时不适合使用的原因。
This has been discussed here: https://github.com/nodejs/node/issues/2098这已经在这里讨论过: https://github.com/nodejs/node/issues/2098
As suggested in the above link, you should use process.kill
to kill the process ( https://nodejs.org/api/process.html#process_process_kill_pid_signal ).正如上面链接中所建议的,您应该使用
process.kill
来终止进程( https://nodejs.org/api/process.html#process_process_kill_pid_signal )。 This should probably work for you:这应该对你有用:
process.kill(-server.pid)
You said that "The child process opened a new terminal window..."您说“子进程打开了一个新终端 window...”
Based on this behaviour, it seems that your OS is Windows.基于此行为,您的操作系统似乎是 Windows。
On Windows, setting
options.detached
totrue
makes it possible for the child process to continue running after the parent exits.在 Windows 上,将
options.detached
设置为true
可以使子进程在父进程退出后继续运行。 The child will have its own console window. Once enabled for a child process, it cannot be disabled.子进程将拥有自己的控制台 window。一旦为子进程启用,就无法禁用。
For process.kill
, the second arg is the signal you want to send.对于
process.kill
,第二个参数是您要发送的信号。 By default, this signal is SIGTERM
.默认情况下,此信号为
SIGTERM
。 However, SIGTERM
doesn't seem to be supported on Windows, according to the Signal Events section of the Node.js docs.但是,根据 Node.js 文档的信号事件部分,Windows 似乎不支持
SIGTERM
。
'SIGTERM'
is not supported on Windows, it can be listened on.Windows 不支持
'SIGTERM'
,可以收听。
Maybe try也许试试
process.kill(server.pid, 'SIGHUP')
or要么
process.kill(server.pid, 'SIGINT')
(This works on macOS but I've not tried it on Windows.) (这适用于 macOS,但我没有在 Windows 上尝试过。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.