This script checks for duplicate ffmpeg
processes running on the system and sends a SIGHUP
signal to the unwanted ones to terminate them. If a process does not terminate after a certain timeout, the script sends a SIGKILL
signal to force the termination.
The script lists out the processes using ps -aux
command and identifies the ffmpeg
processes. The script then groups the processes by their stream id (the streams are initiated to create/encode video feeds) and send termination signals to any duplicate processes.
The script can be used with optional command-line argument -t
or --timeout
which specifies the timeout interval in seconds after which a SIGKILL
signal is sent if the process is still running after receiving a SIGHUP
signal. If no timeout is specified, it defaults to 15 seconds or uses the environment variable FFMPEG_MONITOR_HUP_TIMEOUT
if set.
./ffmpeg_duplicate_process_terminator.py [-t TIMEOUT]
If the -t
option is used with an environment variable, the command line argument is favored.
This script is written in Python3 and uses the following modules:
os
subprocess
time
argparse
signal
re
errno
parse_args()
: Parses command-line arguments.get_timeout(args)
: Retrieves timeout setting from the command-line arguments or environment variable.ps_ef()
: Executesps -aux
command to get the detailed snapshot of the current running processes.get_ffmpeg_processes(ps_ef_output)
: Gets ffmpeg processes with stream_id fromps -aux
output.group_processes_by_stream_id(processes)
: Groups process ids by stream id.is_process_running(pid)
: Checks if a process is still running.kill_duplicate_processes(streams, timeout)
: Kills duplicate ffmpeg processes.main()
: Acts as the entry point when the script is run.
If no ffmpeg
processes are running, the script raises a warning.
The script should be run by a user with privileges to kill processes.
Note: The processes are killed based on the assumption that the only duplicate ffmpeg
processes running would be unwanted. Please only execute the script if you're sure there are no other necessary ffmpeg
processes running on your system.