Mketcha3
Mketcha3

Reputation: 21

Why is process.kill() not killing the process?

I have two separate child processes running, one begins on startup and one "if message === '!exec')". I would like to kill the first child process at the start of the second (again "if message === '!exec'). Everything else is currently running as planned without errors, but unfortunately, child process 1 is not terminating (or maybe it is, but restarts itself?) upon receiving the message.

var exec = require("child_process").exec;
var execFile = require('child_process').exec;

var newproc = execFile('/path/executable, function (error, stdout, stderr) {
    if (error) {
    throw error;
    return;
    }
}); 


client.on('chat', (channel, user, message, self) => { 
    if (message === '!exec') {

    newproc.kill(); //here I am trying to kill child process 1

        exec("/path/runtext.py", (error, stdout, stderr) => {
            if (error) {
                console.error(`exec error: ${error}`);
                return;
            }
            console.log(`stdout: ${stdout}`);
            console.log(`stderr: ${stderr}`);
        });

    }
});

Do you guys have any idea why the process is not being killed/ what I should do to fix it?

Upvotes: 1

Views: 3390

Answers (1)

apocolypse101
apocolypse101

Reputation: 9

.kill doesn't directly kill a process. It sends a SIGTERM to it, which can be ignored when the process is waiting for disk I/O or the network. A better idea would be to send a SIGKILL to the process, but this can be dangerous.

Upvotes: 0

Related Questions