Reputation: 23276
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?
Upvotes: 1
Views: 3362
Reputation: 5941
In fact, unlike the Linux kill()
command, which takes the process pid to kill and a signal as parameters, Nodejs ChildProcess subprocess.kill()
expects only the signal argument (as string according to the documentation).
That makes sense because the subprocess already knows its pid (the one you get when using subprocess.pid
) so you don't need to provide it again.
To kill your process, you can pass a valid signal to the .kill()
method:
ls.kill('SIGTERM');
Or, you could also simply call the .kill()
method of your subprocess without any arguments, which is equivalent to call it with the 'SIGTERM' signal.
ls.kill()
Upvotes: 1
Reputation: 59596
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)
Upvotes: 1