Reputation: 3243
I am using the spawn function with mjpg_streamer. However the child process just exits as soon as it is created. Here is the code I am using
streamingProcess = spawn('mjpg_streamer', [
'-i', '"/usr/local/lib/input_file.so -f ' + IMAGE_PATH + ' -n ' + IMAGE_NAME + '"',
'-o', '"/usr/local/lib/output_http.so -w /usr/local/www"'
]);
streamingProcess.on('exit', function(code, signal) {
console.log('streaming process has stopped. Code: ' + code + ', signal: ' + signal);
streamingProcess = null;
});
running the actual command itself returns the following:
What the mjpg_streamer command essentially does is fire up a webserver. The code in the exit callback is 1, and the signal is null
.
Not quite sure what is going on...
UPDATE:
If I use exec
, then it works fine! However, the process will not die if I call kill!
So
streamingProcess = exec('LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -w /usr/local/www"');
does indeed fire up the webserver, but now if I call
streamingProcess.kill()
the exit event is fired, but the process itself does not die. On further inspection it seems as if the pid's (from the node child process and on the os) are not the same??
Now I am confused...
UPDATE 2:
Ok I think I am starting to understand what is going on with exec
. So it executes the command in a subshell, which for me is creating two processes. One is the subshell, and the other is the mjpg_streamer command itself. So killing the exec
only kills the subshell. But the mjpg_streamer remains.
From node:
From the os:
So 3752 is the actual command I want to kill. I suppose there is no way for me to get this pid from node? :)
Upvotes: 2
Views: 1353
Reputation: 151401
Your spawn
call executes the command but the command fails (exit code 1). I believe this is due to extra quotes appearing in the command you pass to spawn. When you use the shell you have to use double quotes so that the shell won't split arguments that contain spaces. When you are using spawn, you must not use them. So:
streamingProcess = spawn('mjpg_streamer', [
'-i', '/usr/local/lib/input_file.so -f ' + IMAGE_PATH + ' -n ' + IMAGE_NAME,
'-o', '/usr/local/lib/output_http.so -w /usr/local/www'
]);
The only thing I removed in the command above are the double quotes.
And the pid problem with exec
is that Node gets the pid of the shell which starts you command instead of the pid of your command itself. (This also explains why, just like when you type the command at the command line, exec
needs the double quotes to prevent arguments from being split.)
To sum up, spawn
does not use a shell, so you don't use quotes to prevent white-space splitting, and the pid you get is the pid of your command. However, exec
starts a shell to execute the command so quotes are needed, and you get the pid of the shell.
Upvotes: 2