Run FFmpeg in the Background with nohup on Linux
Running long-running media processing tasks in Linux can be
problematic if your terminal session disconnects or closes. This article
provides a quick, practical guide on how to use the nohup
command combined with background operators to keep your FFmpeg processes
running seamlessly in the background, even after you log out.
Understanding the Command Structure
To run FFmpeg in the background and ensure it survives a terminal
detachment, you need to combine nohup, the FFmpeg command
itself, output redirection, and the shell’s background operator.
Here is the standard syntax to achieve this:
nohup ffmpeg -i input.mp4 output.mkv > ffmpeg.log 2>&1 &Breaking Down the Components
nohup: Short for “no hangup.” This utility runs a command immune to hangups (SIGHUP), which is the signal sent to processes when a terminal window is closed or a SSH session drops.ffmpeg -i input.mp4 output.mkv: This is your standard FFmpeg command. Replace this with your specific encoding, multiplexing, or streaming parameters.> ffmpeg.log: Redirects the standard output (stdout) of FFmpeg to a file namedffmpeg.log. This prevents your terminal from being cluttered and saves the progress details.2>&1: Redirects standard error (stderr, stream 2) into standard output (stdout, stream 1). Since FFmpeg writes most of its banner and progress information tostderr, this ensures all relevant logs end up in yourffmpeg.logfile.&: Tells the shell to execute the entire command in the background, immediately returning control of the terminal command line to you.
Managing Your Background FFmpeg Process
Once you execute the command, the terminal will output a Process ID (PID). You can use this ID to monitor or stop the task later.
- Check if it is running: Use the
pscommand to verify that your FFmpeg process is still active.
ps aux | grep ffmpeg- Monitor the progress: Since all output is redirected, you can view the live progress of your render by tracking the log file.
tail -f ffmpeg.log- Terminate the process: If you need to stop the
background encoding before it finishes, use the
killcommand followed by the PID you found during the check phase.
kill [PID_NUMBER]