Search code examples
node.jsshellspawn

Unknown signal error as I try to kill a process


I am trying to kill a process using the kill method in child_process. But I get the following error as I try to call the function:

TypeError [ERR_UNKNOWN_SIGNAL]: Unknown signal: 18408

I am doing as follows:

    const ls = spawn('node',['print']);

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

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

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


    setTimeout(() => {
            ls.kill(ls.pid);
    },4000);

What could be the reason for this? What am I missing?


Solution

  • kill() sends a Unix signal. These are represented by numbers. You should pass the number of a signal to kill(), not the PID.

    Try

    import signal
    print(signal.SIGTERM)
    

    That probably prints 15 which is the number of the signal TERM (which normally terminates the process it is sent to).

    In your program you could call

    ls.kill(signal.SIGTERM)