Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - Spawn and kill a process in node.js

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

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.

My test code is:

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);
});

The result is:

#~$ node test.js
Timeout
kill

But I still need to send ctrl+C to end the program. What am I missing?

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

I 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.

I also launched a ps command while the app was running:

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

So omxplayer is a wrapper, who don t kill it's child process when it end, is there any way to get the pid of the wrapped process?

Still biting dust, tried this:

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

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 that doesn't work.

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

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

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

Which I start like this:

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

Instead of child.kill.

I'm not sure if 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, even better, without having to execute a file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Refer to this discussion

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.

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.

Try changing your setTimeout callback to

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

I hope that should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...