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.
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.
Most helpful comment
If you do need a shell, spawn it with
{ detached: true }. You can then kill the whole process group withprocess.kill(-child.pid).Does not work on Windows and your child processes can of course spawn process groups of their own.