简体   繁体   中英

Nodejs: Childprocess exec kill is not a function

I'm trying to kill a childprocess opened with exec. I can't use spawn since then, the childprocess doesn't seem to work.

Am I missing something? Are childprocesses unkillable?

Thanks in advance

*EDIT: Code

    const child = exec(
     __dirname +
     "/ffmpeg/bin/ffmpeg -i " + url + " -acodec copy -vcodec copy " + savePath,
     function(error, stdout, stderr) {
      console.log("stdout: " + stdout);
      console.log("stderr: " + stderr);
      if (error !== null) {
        console.log("exec error: " + error);
      }
    }
  );

I found that if you try to call kill on a child process inside an event callback it does not have the child process in scope. I'll give you an example:

This will not work:

const parameters = ['-D', '-F', `-W${width}`, `-H${height}`, fileName];
const childProcess = spawn('commandLineProgram', parameters)
.stderr.on('data', (data) => {
    stringToAccumulateData += data;
    if(data.toString().match(/stringLookingFor/)) {
        childProcess.kill();
    }
});

This will work:

const parameters = ['-D', '-F', `-W${width}`, `-H${height}`, fileName];
const childProcess = spawn('commandLineProgram', parameters);

childProcess.stderr.on('data', (data) => {
    stringToAccumulateData += data;
    if(data.toString().match(/stringLookingFor/)) {
        childProcess.kill();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM