Kernel.system doesn't ignore output
I have a small script that I run in WSL (Debian) on ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu].
It does a simple thing:
cmd = ['ffmpeg', '-hide_banner', '-loglevel', 'panic', '-i', "#{video_file}",
'-i', "#{audio_file}", '-c', 'copy', '-map', '0:v:0', '-map', '1:a:0', "#{output}"].join(' ')
@log.debug("Command: #{cmd}")
system(cmd) or raise 'It broke'
As far as I understand, this should cause Ruby to ignore all output from FFmpeg (even though there is supposed to be none), but what happened is FFmpeg was barfing output all over the terminal breaking things.
How is this possible?
I wanted a way to feed array to system() call, so I would specify actual command once as an array and then reuse it.
8 comments · 75% upvoted · other discussions
You can also use Kernel#` to soak up any output, then check
$?(in Ruby, not in the shell) for status. This is my goto method for when I don't care about the shell output; only status. I wrap something like this in arun_cmdmethod that exists in aShellScriptUtilsmodule that I include all over the place.It seems in your other comment you've switched to using Open3, which of course gives you even more control.
Gendalph [OP] 2 points · Oct 2022
In this case this approach is fine, but in general I would rather avoid checking global vars and instead go with something like Open3.