簡體   English   中英

如何使用JavaScript殺死Linux child_process.spawn?

[英]How to kill a linux child_process.spawn using javascript?

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 這是伏都教編碼。

這是一個仍在運行的演示腳本,因此在生成子進程后可以發出信號。 您必須找到一種方法來保持腳本運行,並以某種方式發送“啟動”和“停止”命令,然后將其中繼到子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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM