How to Run FFmpeg as a Daemon Process on Linux
Running FFmpeg in the background as a daemon process on Linux ensures that your media conversion, streaming, or recording tasks continue to run uninterrupted, even after you close your terminal session. This guide provides a straightforward tutorial on how to configure and run FFmpeg as a background service using systemd, which is the standard service manager for most modern Linux distributions, as well as a quick alternative method using command-line tools.
Method 1: Using systemd (Recommended)
Creating a systemd service is the most robust way to run FFmpeg as a daemon. It allows the system to manage the process, automatically restart it if it crashes, and start it automatically upon system boot.
Step 1: Create a systemd Service File
Open your terminal and create a new service configuration file using a text editor. You will need root or sudo privileges:
sudo nano /etc/systemd/system/ffmpeg-daemon.serviceStep 2: Configure the Service
Paste the following configuration into the file. Make sure to replace
/usr/bin/ffmpeg with the actual path to your FFmpeg binary
(you can find this by running which ffmpeg) and customize
the input/output arguments for your specific use case:
[Unit]
Description=FFmpeg Daemon Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/ffmpeg -re -i http://example.com/live/stream.m3u8 -c copy /var/www/html/stream.mp4
Restart=always
RestartSec=5
User=nobody
Group=nogroup
[Install]
WantedBy=multi-user.targetKey directives in this configuration: * ExecStart: The exact command FFmpeg will run. Always use absolute paths for the executable and output files. * Restart=always: Automatically restarts the FFmpeg process if it stops or crashes. * User/Group: Runs the process under a non-root user for security.
Save and close the file (in Nano, press Ctrl+O,
Enter, then Ctrl+X).
Step 3: Enable and Start the Daemon
Reload the systemd manager configuration to recognize the new service:
sudo systemctl daemon-reloadStart the FFmpeg service:
sudo systemctl start ffmpeg-daemonEnable the service to start automatically at boot:
sudo systemctl enable ffmpeg-daemonStep 4: Manage the Service
To check the status of your daemon and ensure it is running successfully:
sudo systemctl status ffmpeg-daemonTo stop or restart the daemon:
sudo systemctl stop ffmpeg-daemon
sudo systemctl restart ffmpeg-daemonTo view the output logs generated by FFmpeg:
journalctl -u ffmpeg-daemon.service -fMethod 2: Using nohup (Quick Alternative)
If you need a quick, temporary way to run FFmpeg in the background
without creating a systemd service, you can use the nohup
command combined with an ampersand (&). This prevents
the process from terminated when you log out.
Run the following command:
nohup ffmpeg -i input.mp4 output.mkv > ffmpeg.log 2>&1 &- nohup: Ignores the HUP (hangup) signal sent to the process when the terminal closes.
- > ffmpeg.log 2>&1: Redirects standard output and standard error to a log file.
- &: Runs the command in the background.
To stop a process started with nohup, you must find its
Process ID (PID) and terminate it manually:
pgrep -f ffmpeg
kill <PID>