How to Force Kill FFMPEG Process to Restore Apache Server Performance on Ubuntu


10 views

When FFMPEG hogs system resources, the first step is identifying the rogue process. SSH into your server and run:

ps aux | grep ffmpeg

This will show output like:

user     12345  120  5.2 987654 123456 ?       Rl   Mar31 120:30 ffmpeg -i input.mp4 output.avi

Once you've identified the PID (12345 in our example), you have several termination options:

Graceful termination:

kill -15 12345

Force kill if unresponsive:

sudo kill -9 12345

For more control, use htop for interactive process management:

sudo apt install htop
htop

Navigate to the FFMPEG process with arrow keys and press F9 to kill it.

Consider using nice and ionice when starting FFMPEG:

nice -n 19 ionice -c 3 ffmpeg -i input.mp4 output.avi

After killing FFMPEG, restart Apache to ensure clean state:

sudo service apache2 restart

Install monitoring tools to prevent future issues:

sudo apt install sysstat
sar -u 1 5  # Shows CPU usage every 1 second for 5 intervals

When FFMPEG processes consume excessive resources on an Ubuntu server, you'll want to first identify the exact process IDs. SSH into your server and run:

ps aux | grep ffmpeg

This will show output like:

user     12345  180  5.2 487612 1048576 ?     Rl   Mar01 120:45 ffmpeg -i input.mp4 output.avi

There are several methods to terminate FFMPEG processes:

# Graceful kill (SIGTERM)
sudo kill 12345

# Forceful kill (SIGKILL)
sudo kill -9 12345

# Kill all FFMPEG processes
pkill -9 ffmpeg

After killing FFMPEG, check system resources to confirm recovery:

top -c
htop
vmstat 1

Consider implementing process limits:

# Install cpulimit
sudo apt-get install cpulimit

# Limit FFMPEG CPU usage
cpulimit -l 50 -p 12345

Alternatively, use nice to prioritize processes:

nice -n 19 ffmpeg -i input.mp4 output.avi

Create a watchdog script (/usr/local/bin/ffmpeg_watchdog.sh):

#!/bin/bash
THRESHOLD=90
PROCESS="ffmpeg"

while true; do
    CPU_USAGE=$(top -bn1 | grep "$PROCESS" | head -1 | awk '{print $9}')
    if (( $(echo "$CPU_USAGE > $THRESHOLD" | bc -l) )); then
        pkill -9 "$PROCESS"
        echo "$(date) - Killed $PROCESS with CPU $CPU_USAGE%" >> /var/log/ffmpeg_watchdog.log
    fi
    sleep 30
done

Make it executable and add to cron:

chmod +x /usr/local/bin/ffmpeg_watchdog.sh
(crontab -l ; echo "@reboot /usr/local/bin/ffmpeg_watchdog.sh") | crontab -

For more robust control, create a systemd service:

# /etc/systemd/system/ffmpeg.service
[Unit]
Description=FFMPEG Encoding Service
After=network.target

[Service]
User=media
Group=media
ExecStart=/usr/bin/ffmpeg -i /path/to/input /path/to/output
Restart=on-failure
CPUQuota=80%
MemoryLimit=1G

[Install]
WantedBy=multi-user.target

Then manage with:

sudo systemctl daemon-reload
sudo systemctl start ffmpeg
sudo systemctl status ffmpeg