在node.js中生成并终止进程 [英] Spawn and kill a process in node.js

查看:287
本文介绍了在node.js中生成并终止进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会因此而被投票,但我不明白我哪里出错。

I might be downvoted for this, but I don't understand where I'm going wrong.

我正在尝试在javascript中生成一个进程,并且一段时间后杀死它(用于测试)。

I'm trying to spawn a process in javascript, and kill it after some time (for test).

最后,该过程将是一个无限循环,我需要在指定时间使用不同的参数重新启动,所以我认为产生这个过程并杀死它是最好的方法。

In the end, the process will be a infinite loop that I need to restart with different arguments at specified time, so I thought that spawning the process and killing it was the best way to do this.

我的测试代码是:

var spawn=require('child_process').spawn
, child=null;

child=spawn('omxplayer', ['test.mp4'], function(){console.log('end');}, {timeout:6000});
console.log('Timeout');
setTimeout(function(){
    console.log('kill');
    child.kill();
}, 1200);

child.stdout.on('data', function(data){
    console.log('stdout:'+data);
});

child.stderr.on('data', function(data){
    console.log('stderr:'+data);
});

child.stdin.on('data', function(data){
    console.log('stdin:'+data);
});

结果是:

#~$ node test.js
Timeout
kill

但是我仍然需要发送ctrl + C来结束程序

But I still need to send ctrl+C to end the program

我缺少什么?

编辑:
在Raspbian,节点0.10.17,omxplayer是二进制(视频播放器)。

On Raspbian, node 0.10.17, omxplayer is a binary (video player).

尝试:
添加chmod + x到该应用程序。
以root身份发布。
暂停子进程的标准输入。在kill命令中使用所有与终止相关的信号。

Tried: Added chmod +x to the app. Launched as root. Paused stdin of the child process. Using all terminate-related signal in the kill command.

EDIT2:

在应用程序中启动ps命令正在运行:

Launched a ps command while the app was running:

2145    bash
2174    node
2175    omxplayer
2176    omxplayer.bin
2177    ps

所以omxplayer do是一个包装器,它在结束时不会结束进程,有没有办法得到包装过程的pid?

So omxplayer do is a wrapper, which don t end the process when it end, is there any way to get the pid of the wrapped process?

EDIT3:

仍然咬着灰尘,试过这个:

Still biting dust, tried this:

spawn('kill', ['-QUIT', '-$(ps opgid= '+child.pid+')']);

我认为会杀死omxplayer的所有孩子,我不知道是否使用了这样的spawn是错误的或者如果它是不起作用的代码。

Which I thought would kill all children of omxplayer, I don t know if using spawn like that is wrong or if it s the code which don t work.

最后编辑:

我做的最后一次编辑是好的答案,但不得不被欺骗。

The last edit I made was the good answer, but had to be tricked a bit.

我创建了一个sh文件(执行权限),如下所示:

I created a sh file (with execute right) like this:

PID=$1
PGID=$(ps opgid= "$PID")
kill -QUIT -"$PGID"

我从这样开始:

execF('kill.sh', [child.pid], function(){
    console.log('killed');
});

而不是child.kill。

Instead of child.kill.

我不确定这是最好的方法,也不是代码是干净的,但它确实有用。

I m not sure it s the best way to do, nor if the code is clean, but it does work.

我会接受任何使它成为清洁工的答案方式或最好的,无需执行文件。

I ll accept any answer which make it in a cleaner way or, the best, without having to execute a file.

推荐答案

请参阅此讨论

一旦开始在stdin上侦听数据,节点将等待stdin上的输入,直到被告知不要。当用户按下ctrl-d(表示输入结束)或程序调用stdin.pause()时,节点停止等待stdin。

Once you start listening for data on stdin, node will wait for the input on stdin until it is told not to. When either user presses ctrl-d (meaning end of input) or the program calls stdin.pause(), node stops waiting on stdin.

节点程序不退出除非它无所事事或等待。发生的事情是,它正在等待stdin,因此永远不会退出。

A node program does not exit unless it has nothing to do or wait for. Whats happening is, it is waiting on stdin and therefore never exits.

尝试将setTimeout回调更改为

Try changing your setTimeout callback to

console.log('kill');
child.stdin.pause();
child.kill();

我希望这应该有效。

这篇关于在node.js中生成并终止进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆