Saturday 22 August 2020

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. Can someone point out what I have done wrong?



from Kill detached child process in Node

No comments:

Post a Comment