Restarting FFMPEG Using Monit

Restarting FFMPEG Using Monit

 

How to monitoring and restarting ffmpeg using “Monit” utility.

Monit is a free, open source process supervision tool for Unix and Linux. With Monit, system status can be viewed directly from the command line, or via the native HTTP(S) web server.

Create FFMPEG Start Script

We will start our ffmpeg from script – starter.sh. Create new file starter.sh and fill with code:

#!/bin/bash
pid_file="/var/run/transcode.pid"
killall -9 ffmpeg
ffmpeg -v 0 -loglevel 0 -i StreamSource Your_Ffmpeg_Commands udp://225.2.1.11:1300 &
ffmpeg_pid=$!
echo $ffmpeg_pid
echo $ffmpeg_pid > $pid_file
>>/dev/null
exit 0

Change ffmpeg -v 0 -loglevel 0 -i StreamSource Your_Ffmpeg_Commands udp://225.2.1.11:1300  row content as you need.

Install and Configure Monit

For Ubuntu/Debian/Mint:

$ sudo apt-get install monit

On CentOS/RedHat/Fedora:

# yum install monit

Start Monit:

# service monit start

Display Monit’s Status Details:

# monit status

Example output:

The Monit daemon 5.5 uptime: 1h 24m

Process 'ffmpeg'
status Running
monitoring status Monitored
pid 17650
parent pid 1
uptime 1h 23m
children 0
memory kilobytes 53200
memory kilobytes total 53200
memory percent 1.3%
memory percent total 1.3%
cpu percent 8.3%
cpu percent total 8.3%
data collected Thu, 07 Jun 2018 15:06:06

System '127.0.0.1'
status Running
monitoring status Monitored
load average [0.34] [0.29] [0.21]
cpu 8.7%us 1.1%sy 0.0%wa
memory usage 247776 kB [6.1%]
swap usage 0 kB [0.0%]
data collected Thu, 07 Jun 2018 15:06:06

 

Edit the main configuration file of Monit with any text editor. Configuration file located at /etc/monit.conf (RedHat/CentOS) or /etc/monitrc (Ubintu).

In configuration below Monit will monitoring 2 things: ffmpeg process availability and CPU (user) usage. If no ffmpeg process in the system Monit will start starter.sh script (ffmpeg restart), or if CPU usage goes below 5% for 2 testing cycles ffmpeg will be restarted.

And add next text.  Monit will write logs in monit.log file. Change “udp://225.2.1.11:1300” to yours destination. PATH_TO_START_SCRIPT – patch to your ffmpeg start script starter.sh. Also you will need change  CPU usage percent for your case. set daemon 3 this is the 3 seconds interval Monit runs its checks.

# vi /etc/monit.conf

set daemon 3 
set logfile /var/log/monit.log
check process ffmpeg
matching "udp://225.2.1.11:1300"
start program = "PATH_TO_START_SCRIPT/starter.sh"
stop program = "/usr/bin/killall -9 ffmpeg"

check system 127.0.0.1
start program = "PATH_TO_START_SCRIPT starter.sh"
stop program = "/usr/bin/killall -9 ffmpeg"
if cpu usage (user) < 5% for 2 cycles then restart

 

After installing and configuring Monit, check  configuration file syntax for errors.

# monit -t

If everything is OK command will return: “Control file syntax OK“.

Now you can need restart Monit for reloading new configuration:

# service monit restart

Log file output example how Monit restarts ffmpeg process when CPU usage below configured threshold:

Monit Restarting FFMPEG

 

arstech

Leave a Reply