如何使用javascript杀死linux child_process.spawn

How to kill a linux child_process.spawn using javascript?

本文关键字:process spawn child linux 何使用 javascript 杀死      更新时间:2023-09-26
var child_process = require('child_process');
if (typeof ffmpegrec == "undefined" || ffmpegrec == null)
{
    var ffmpegrec = '';
}

if(record === "1")
{
    ffmpegrec = child_process.spawn("ffmpeg",[
        "-re",                   // Real time mode
        "-f","x11grab",          // Grab screen
        "-framerate","60",              // Framerate
        "-s",width+"x"+height,   // Capture size
        "-i",":"+screen+"+"+top+","+left,        // Capture offset
        "-vb","1024k",            // Target bit rate
        "-f","mpeg1video",             // File format
        "/home/CloudGaming/webContent/recordedVideos/test.mpg"]);
}
else if(record === "0")
{
    ffmpegrec.kill();
}

我发送信号 1 录制视频流,而 0 停止视频流。录音就像一个魅力,但这个过程不能被杀死。请帮忙。提前感谢!

我正在采取一种更大的方法来展示如何使用 spawn(( 等来管理进程。

我认为您犯了一个根本错误,因为您将其作为脚本运行以完成以启动该过程,并期望能够再次运行它以停止该过程。当脚本第一次停止时,ffmpegrec不再有任何意义;JavaScript 引擎及其所有全局变量都被处理掉了。所以在第二次运行时,现在您将ffmpegrec初始化为空字符串,这样它就是某种Object。这是巫毒教编码。

下面是一个保持活动状态的演示脚本,因此它可以在生成子进程后发出信号。您必须找到一种方法来保持脚本运行,并以某种方式发送"start"和"stop"命令,然后将其中继到子ffmpeg进程。这是一个由您决定的设计挑战。 然后,你必须使用适当的错误和输出侦听器来处理子进程,你需要使用函数抽象,将所有变量设置在正确的位置,不要混合类型,并检查错误。 以下作品。看看这是否为您理顺了一切。

var child_process = require('child_process');
var ffmpegrec = null;
function record(setting) {
    if ( setting === 1 ) {
        if ( ffmpegrec !== null ) {
            console.log('Error! ffmpeg already running');
        }
        else {
            // I don't have ffmeg installed, so simulating a long process instead                                                                      
            ffmpegrec = child_process.spawn('sh', ['-c', 'sleep 10']);
            ffmpegrec.on('close', function(code, signal) {
                console.log('child process terminated due to receipt of signal ' + signal + ' or code ' + code);
                ffmpegrec = null;
            });
            ffmpegrec.stdout.on('data', function (data) {
                console.log('stdout: ' + data);
            });
            ffmpegrec.stderr.on('data', function (data) {
                // important to monitor ffmegprec's complaints
                console.log('stderr: ' + data);
            });
            // Can track using "ps" unix util, etc.
            console.log('Initiated recording on PID ' + ffmpegrec.pid);
        }
    }
    else if ( setting === 0 ) {
        if ( ffmpegrec === null ) {
            console.log('Error! Tried to stop recording and no recording active');
        }
        else {
            ffmpegrec.kill('SIGINT');
            // ffmpegrec will become null if and when signal delivery succeeds
            console.log('Terminated recording');
        }
    }
    else {
        console.log('Error! Invalid value for setting: ' + setting);
    }
}

console.log('Starting');
record(0); // Can't stop if not yet started; 1st error                                                                                                 
record(1); // OK                                                                                                                                       
record(1); // Can't start if already started; 2nd error                                                                                                
setTimeout(function() { record(0); /* OK to stop now */ }, 5000);