Quick note: Monit-er FFMPEG : run ffmpeg as daemon

On CentOS 6, I just set up a script and monit to rerun ffmpeg if it crashes.  We’re using ffmpeg to push video from an IP camera to a CDN.

Screenshot from 2017-04-10 11-51-54FFMPEG

Install NUX repo in order to get FFMPEG:

$ rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm

Note: the el6 + x86_64

$ yum install ffmpeg

Monit

Monit will monitor FFMPEG and restart it if FFMPEG crashes.

$ yum install monit

User

Please, don’t run this as root. Create a user “scriptcam”:

$ adduser -d /usr1/home/scriptcam -c 'Script Camera' scriptcam

Create script ScriptCam

In /usr/local/sbin/ScriptCam:

#/usr/bin/bash
pid=`runuser -l scriptcam -c 'echo|/usr/bin/ffmpeg -v 0 -i rtsp://x.x.x.x/channels/1 -r 25 -vcodec copy -acodec copy -f flv rtmp://y.y.y.y/zzzzzzz-live/zzzzzzz01 >/dev/null 2>&1 & echo $!'`
echo $pid > $1

Create Init script for FFMPEG

Install to /etc/init.d/ffmpeg:

#!/bin/sh
#
# ffmpeg Startup script for ffmpeg/ScriptCam
#
# chkconfig: - 85 15
# description: Push ScriptCam video to CDN
# processname: ffmpeg
# pidfile: /var/run/ffmpeg-daemon.pid
#
### BEGIN INIT INFO
#
# Provides : ffmpeg
# Required-Start : $local_fs $remote_fs $network $named
# Required-Stop : $local_fs $remote_fs $network
# Default-Start : 2 3 4 5
# Default-Stop : 0 1 6
# Short-Description : ffmpeg daemon
# Description : ffmpeg daemon
#
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

prog=ffmpeg
pidfile=${PIDFILE-/var/run/ffmpeg-daemon.pid}
OPTIONS=$pidfile
lockfile=${LOCKFILE-/var/lock/subsys/ffmpeg}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

if [ "$(id -u)" != "0" ]; then
 echo "This script must be run as root"
 exit 1
fi

start() {
 echo -n $"Starting $prog: "
 LANG=$HTTPD_LANG daemon --pidfile=${pidfile} /usr/local/sbin/FalconCam $OPTIONS
 RETVAL=$?
 echo
 [ $RETVAL = 0 ] && touch ${lockfile}
 return $RETVAL
}

stop() {
 echo -n $"Stopping $prog: "
 killproc -p ${pidfile} -d ${STOP_TIMEOUT} $prog
 RETVAL=$?
 echo
 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

case "$1" in
 start)
   start
 exit 0
 ;;
 stop)
   stop
 exit 0
 ;;
 restart)
   stop
  start
 exit 0
 ;;
 **)
 echo "Usage: $0 {start|stop|restart}" 1>&2
 exit 1
 ;;
esac

Monitor with Monit

Use monit to monitor the ffmpeg process. Monit will restart the falcon cam if ffmpeg crashes.

Create /etc/monit.d/ffmpeg:

check process ffmpeg with pidfile /var/run/ffmpeg-daemon.pid
      start program = "/etc/init.d/ffmpeg start" with timeout 60 seconds
      stop program = "/etc/init.d/ffmpeg stop"

At Boot

Configure monit to start at boot:

$ chkconfig monit on

Configure ffmpeg to start at boot:

$ chkconfig –add ffmpeg
$ chkconfig ffmpeg on

Extras

$ service ffmpeg restart # stop | start
$ service monit restart # stop | start

Check the monit web site (the user details are in /etc/monit.conf) :

http://localhost:2812/

About c3iq

Opensource, Linux, Unix, Fish, Family
This entry was posted in Development, ITMS, Linux SysAdmin, university and tagged , , , , , . Bookmark the permalink.