ffmpeg hangs when run in background

It hangs because after a certain point it can not write to it’s output pipe any longer.

When you run a process it has 3 opened pipes for: stdin, stdout and stderr. A pipe has a memory buffer ( 4KB on Linux ) that can hold up to a certain amount of data and the next write operation will pause until some data is read from the other side of the pipe.

Since you never read from stdout and stderr of your child process and FFMpeg outputs quite a lot it will hang at some point.

As explained in the comment above you can simply redirect your ffmpeg output to /dev/null using:

ffmpeg .... > /dev/null 2>&1 < /dev/null

In this case ffmpeg will never output enough data to have the pipe ‘hang’.

Another option would be to just close stdin, stdout and stderr of your child process as soon as you launch it.

And yet another option would be to actually read ( and optionally discard ) everything on stdout and stderr of your child process.

Leave a Comment