Node.js exec: best way of killing a child pipe and all of its children

I have a bash command that pipes an audio stream from avconv/ffmpeg to another bash script which must be run with sudo:

avconv -i http://8273.live.streamtheworld.com:3690/WQHTFMAAC_SC -f wav -ac 1 -ar 22050 - | sudo ./pifm -

The node.js script is used to stop the script from time to time:

var exec = require("child_process").exec,
    signal = null;

var signal = exec("avconv -i http://8273.live.streamtheworld.com:3690/WQHTFMAAC_SC -f wav -ac 1 -ar 22050 - | sudo ./pifm -")
setTimeout(function() {
    signal.kill('SIGINT')
},10000)

Unfortunately, this doesn't kill either of the processes in the pipe.

A few further bits of info:

  • The resultant PID of the piped process (console.log(signal.pid)) doesn't represent the PID's of either process, which I presume is related to the fact that it's a pipe?
  • I was able to reliably kill the first child (avconv) by adding one to the signal.pid, but the PID of the second process (presumably because it's sudo) was not relative to the signal.pid and therefore impossible to reliably kill without concocting an elaborate top | grep syntax
  • I also tried killing the process using the node process module:

    process.kill(this.current.pid,'SIGINT');
    

    which didn't work either.

  • I'd prefer to avoid using Node to pipe between the two processes, as I'm running this in a low memory environment

I suspect that piped bash processes are unique in some way, but I'm a bit out of my league with the bash end of things!

I just remembered I had a similar problem and solved it by using awk to push the command,

awk '{system("avconv -i http://8273.live.streamtheworld.com:3690/WQHTFMAAC_SC -f wav -ac 1 -ar 22050 - | sudo ./pifm -")}'
PID=$!


setTimeout(function() {
    sudo kill $PID #(Can try to force it with kill -9 instead)
},10000)

This way you really kill the process that owns the command. I'm not sure it will work in your context tough. Farewell friend hope I helped you out!

Try

process.kill(-signal.pid);

to terminate the whole process group.