Search code examples
node.jsexecvlcrtmp

nodejs exec keep shell alive?


I'm trying to run a command that opens VLC player and streams RTMP video into it. But when I run it, VLC player comes up and then closes. I think this is because the shell it relies on is being killed. How can I keep the shell alive?

Running the same command from cmd line in windows works fine. And closing the shell closes VLC player.

My code looks like this

var exec = require('child_process').exec
exec('"Z:/downloads/rtmpdump-2.4-git-010913-windows/rtmpdump.exe" -r "rtmp://199.9.251.83/app/jtv__cQlY3imWtyDf4Cr" -j "ae9d2c8bbe586ac9f88a4fae9ec5841c3976092c:{\"swfDomains\": [\"justin.tv\", \"jtvx.com\", \"xarth.com\", \"twitchtv.com\", \"twitch.tv\", \"newjtv.com\", \"jtvnw.net\", \"wdtinc.com\", \"imapweather.com\", \"facebook.com\", \"starcrafting.com\"], \"streamName\": \"jtv__cQlY3imWtyDf4Cr\", \"expiration\": 1370153205.846189, \"geo_ip\": \"91.10.111.130\", \"server\": \"video3-1\"}" --swfVfy "http://www-cdn.jtvnw.net/widgets/live_embed_player.r4b02d38f442d7cae5646f1a1c17078362f5fd857.swf?channel=games&referer=&channel=games" -v -o - | "C:/Program Files (x86)/VideoLAN/VLC/vlc.exe" - --play-and-exit')

Solution

  • You specified Node.js, but didn't provide an example of node.js code. Should we assume exec(command); is supposed to represent require('child_process').exec(command);?

    If so, you may want to use childprocess.spawn instead. Exec has limitations and is intended for short-running process which output textual data via stdio. If the output from the exec'd process exceeds the pre-allocated buffer, it will terminate (not necessarily your problem, but a possibility). Spawn is intended for longer-running processes (they can even be detached from the parent in some cases).

    If it still closes immediately, then the problem is most likely not due to the shell being terminated prematurely. Let us know.

    EDIT:

    To address your comment:

    Basically you would break up the arguments based on where the spaces occur (except where an argument is wrapped in quotes, which would be considered a single argument). You would also have to do the pipe manually, but there's an example of how to do that under the spawn documentation.

    Another alternative would be to simply generate a shell script file, and then use node to execute that script.