title: Restarting ffmpeg process using monit
body: <p>I am trying to use monit for monitoring the ffmpeg process. For this I first created a bash script that has 3 arguments(start, stop and restart). When I run the script manually from the terminal <code>(./thisBashScript start, ./thisBashScript stop and ./thisBashScript restart )</code> everything works perfectly how it was designed. The process starts, creates and saves the current procees id into a pid file. After if I want to stop the process it takes the current pid file and and kills the process with the given pid. Restarts also works fine, first it stops and after starts the process.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>I installed monit for monitoring the process in case it goes down. I configured to check the process in the exact manner as I configured nginx that excellent works with monit.</p>
  
  <p>After, when I start the process with the command <code>./thisBashScript start</code> monit starts monitoring. In monit status the process ffmpeg is showing as running. After I kill the process manually using the pid of ffmpeg process to test if monit restarts the process, it fails to do so. Even though the new pid is created and saved in <code>/var/run/ffmpeg.pid</code>.</p>
  
  <p>Here are the monit logs:</p>
  
  <pre><code>[EET Mar 21 12:12:37] error    : 'ffmpeg' process is not running
  [EET Mar 21 12:12:37] info     : 'ffmpeg' trying to restart
  [EET Mar 21 12:12:37] info     : 'ffmpeg' start: /etc/init.d/iptv/thisBashScript
  </code></pre>
  
  <p>I have the following bash script:</p>
  
  <pre><code> #!/bin/sh
  
  pid_file="/var/run/ffmpeg.pid"
  
  case "$1" in
   restart)
      /etc/init.d/iptv/thisBashScript stop
      /etc/init.d/iptv/thisBashScript start
          ;;
  
   start)
      rm $pid_file
          ffmpeg -i udp://@someIp:1234 -acodec libmp3lame -ac 1 -ar 44100 -ab 64k -s 640x360 -deinterlace -vcodec h264_qsv -vb 700k -f flv rtmp://someIp/applicationName/360 &amp;
      ch_pid=$! 
      echo "Start HLS: ffmpeg = $ch_pid";
      echo $ch_pid &gt; $pid_file
           ;;
   stop)
      echo "Stop transcoding";
          kill `cat $pid_file`
           ;;
  
          *)
      echo "Usage: /etc/init.d/thisBashScript {start|stop|restart}"
           exit 1
           ;;
   esac
  exit 0
  echo $pid_file
  </code></pre>
  
  <p>This bash script can accept 3 arguments (start, restart and stop)</p>
  
  <ul>
  <li><p>start (it starts the ffmpeg command that streams a video on an nginx rtmp server);</p></li>
  <li><p>stop (it stops the ffmpeg command);</p></li>
  <li><p>restart (it stops and then starts ffmpeg command);</p></li>
  </ul>
  
  <p>Here is my monit configuration</p>
  
  <pre><code>check process ffmpeg with pidfile /var/run/ffmpeg.pid
      start program = "/etc/init.d/iptv/thisBashScript start"  
      stop program = "/etc/init.d/iptv/thisBashScript stop" 
  </code></pre>