Node: Cannot kill child processes.

Created on 13 Jun 2016  路  5Comments  路  Source: nodejs/node

  • Version: 6.2.1
  • Platform: Win10 x64
  • Subsystem: Child_Processes

Hello, before I explain my problem, let me ask this is a really plain and basic way.
If I have

function submitBtn() {
    var proc = require("child_process"),
    cmd = 'ffmpeg -y -i input.mp4 output.mp3',
    func = proc.exec(cmd, function(err, stdout, stderr) {})
}

Example 1: Let's say that input.mp4 is one minute long. So it runs func, then completes in a very short amount of time and exits the process completely.
Example 2: Let's say that input.mp4 is two hours now. Runs the func, but takes more than a minute to complete. Is there a way to force close the process before it completes? This wouldn't be inside submitBtn as this would be another button, so it'd be inside another function.

I keep getting people saying to use process.on("exit"...) but that only runs after the program is done. I'm at a loss for this and spent around a day and half for this explanation.

child_process question

Most helpful comment

If you do need a shell, spawn it with { detached: true }. You can then kill the whole process group with process.kill(-child.pid).

Does not work on Windows and your child processes can of course spawn process groups of their own.

All 5 comments

As long as you keep a reference to your child process object (func in your case), you can call func.kill() to kill the process early.

It says it killed it, but ffmpeg.exe stays open in the background (viewable from task manager) and runs until the conversion is complete.

@ErraticFox Try starting the child process with execFile() or spawn() instead of exec(). The former two do not spawn a shell, which may be what is being killed instead of the actual target process.

Worked perfectly for execFile(). Thanks!

If you do need a shell, spawn it with { detached: true }. You can then kill the whole process group with process.kill(-child.pid).

Does not work on Windows and your child processes can of course spawn process groups of their own.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cong88 picture cong88  路  3Comments

srl295 picture srl295  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

willnwhite picture willnwhite  路  3Comments

Icemic picture Icemic  路  3Comments