Reputation: 1732
I'm writing a pretty simple music server for my Raspberry Pi that will play MP#s on-demand. I can successfully launch an instance of mpg123
using child_process.exec()
, but the PID of this new process differs from the process actually decoding the music. This is important to me because I want to track the process in the event of a user wanting to stop the job (child.kill()
). I'm executing as follows:
var child = child_process.exec('mpg123 "' + filename + '"');
console.log(JSON.stringify({pid: child.pid});
This outputs {"pid":9444}
, and music starts playing. However, when I check ps aux | grep mpg123
, I see two different processes:
pi 9444 0.0 0.1 1760 504 pts/1 S+ 18:55 0:00 /bin/sh -c mpg123 "/home/pi/test.mp3"
pi 9445 11.0 0.6 14840 3112 pts/1 S+ 18:55 0:00 mpg123 /home/pi/test.mp3
This latter process, 9445, is what's actually playing my mp3. If I hang onto child
and .kill()
upon request, the first job is killed but the second lingers, making the kill ineffective.
Is there a different way to create a process that avoids this, or some better way to kill it? I'd rather not have to track everything (eg, the command I used to create it) and try to track down the 'correct' one based on that (eg, ps aux | grep <filename>
).
Upvotes: 0
Views: 1265
Reputation: 1732
According to this page, child_process.execFile(cmd, [args])
will spawn a new subshell, which is the pid of the .exec
'd process. The subshell then created my instance of mpg123
, which is the process I wanted to kill. Instead, I use:
var child = child_process.execFile('mpg123', [filename]); // note filename isn't enquoted
And when I want to kill it, I can simply:
child.kill(); // works
Upvotes: 1