如何使用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
。这是巫毒教编码。
下面是一个保持活动状态的演示脚本,因此它可以在生成子进程后发出信号。您必须找到一种方法来保持脚本运行,并以某种方式发送"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);
相关文章:
- 如何同步写入process.stdout
- Node.js-等待process.exit()在前面的代码完成后执行
- nodejs:child_process.spawn未报告退出代码
- 将process.env变量从节点传递或使用到reactjs
- 如何使用javascript杀死linux child_process.spawn
- PhantomJs:Spawn没有打开任何应用程序
- 在Node.js中使用setImmediate()和process.nextTick()的意义
- 命令行上的输入在什么时候解析为 node.js 中的 process.argv
- 是什么原因导致process.hrtime()挂在nodejs中
- Express+PassportJs:为什么我们需要在passport策略中延迟process.nextTick()方法
- 节点0.10中未定义Process.domain
- JSON值使用process.env[node.js]动态更新
- “process.nextTick”如何防止我的堆栈爆炸
- spawn() 函数的“关闭”回调在 grunt 插件中不起作用
- 节点.js spawn:控制台.log 不记录两次,process.stdin.on('data',ca
- 节点服务器响应错误: process.nextTick(function(){throw err;});.
- process.nextTick的常用方法
- 为什么process.nextTick()在这个“假异步函数”中触发了一个TypeError,而setTimeout()
- 节点.JS错误 - process.env.NODE_TLS_REJECT_UNAUTHORIZED.这是什么意思
- process.on('uncaughtException')的令人困惑的事件发射器/闭包模式