title: Restart ffmpeg process using monit if TOTAL CPU is less than 1%
body: <p>I have used kind of similar solution like this <a href="https://stackoverflow.com/questions/36128450/restarting-ffmpeg-process-using-monit/41274415">Restarting ffmpeg process using monit</a> to restart my ffmpeg stream in case it fails for some reason. Remember its not duplicate problem/question, because I have other issues unlike the example question/solution <a href="https://stackoverflow.com/questions/36128450/restarting-ffmpeg-process-using-monit/41274415">Restarting ffmpeg process using monit</a>, which I'm gonna explain below. So here is my monit configuration:</p>
  
  <pre><code>    check process FFMPEGStream with pidfile PATH-to-file/streampid.pid
      start program = "PATH-to-file/streambash.sh restart"
      stop program = "PATH-to-file/streambash.sh stop"
      if TOTAL CPU is less than 1% for 10 cycles then restart
  </code></pre>
  
  <p>Here is my streambash.sh file:</p>
  
  <pre><code>    #!/bin/bash
      pid_file="PATH-to-file/streampid.pid"
  
      case "$1" in
       restart)
          PATH-to-file/streambash.sh stop
          PATH-to-file/streambash.sh start
             ;;
  
       start)
          rm $pid_file
          /usr/bin/ffmpeg -i "INPUT-PATH" -c:v libx264 -b:v 900k -preset ultrafast -aspect 16:9 -s 640x376 -strict experimental -c:a aac -b:a 96k -f flv "RTMP-PATH" &amp;&gt; /dev/null &amp;
          ch_pid=$! 
          echo "Start Stream1: ffmpeg = $ch_pid";
          echo $ch_pid &gt; $pid_file
             ;;
  
       stop)
          echo "Stop ffmpeg Stream1";
          kill `cat $pid_file` &amp;&gt; /dev/null
             ;;
  
       *)
          echo "Usage: PATH-to-file/streambash.sh {start|stop|restart}"
          exit 1
             ;;
  
       esac
      exit 0
      echo $pid_file
  </code></pre>
  
  <p>Monit can start the bash file successfully, but when this condition <i>"if TOTAL CPU is less than 1% for 10 cycles then restart"</i> is matched in monit configuration, it tries to restart, it gives error that process is not running. But in actual the ffmpeg process still runs in the background and I can see that the stream is live on my website. Here is monit logs:</p>
  
  <pre><code>    [CET Jan 10 12:55:02] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:07] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:12] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:17] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:22] error    : 'FFMPEGStream' total cpu usage of 0.9% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:27] error    : 'FFMPEGStream' total cpu usage of 0.9% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:32] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:37] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:42] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:47] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage&gt;1.0%]
      [CET Jan 10 12:55:50] info     : 'FFMPEGStream' trying to restart
      [CET Jan 10 12:55:50] info     : 'FFMPEGStream' stop: PATH-to-file/streambash.sh
      [CET Jan 10 12:55:51] info     : 'FFMPEGStream' start: PATH-to-file/streambash.sh
      [CET Jan 10 12:55:56] error    : 'FFMPEGStream' process is not running
      [CET Jan 10 12:55:58] info     : 'FFMPEGStream' trying to restart
      [CET Jan 10 12:55:58] info     : 'FFMPEGStream' start: PATH-to-file/streambash.sh
      [CET Jan 10 12:56:04] error    : 'FFMPEGStream' process is not running
      [CET Jan 10 12:56:04] info     : 'FFMPEGStream' trying to restart
      [CET Jan 10 12:56:04] info     : 'FFMPEGStream' start: PATH-to-file/streambash.sh
      [CET Jan 10 12:56:09] error    : 'FFMPEGStream' process is not running
      [CET Jan 10 12:56:09] info     : 'FFMPEGStream' trying to restart
      [CET Jan 10 12:56:09] info     : 'FFMPEGStream' start: PATH-to-file/streambash.sh
  </code></pre>
  
  <p>Monit keeps trying to restart the process and on each retry, it dumps a new pid to the PATH-to-file/streampid.pid, but as I said it seems, it somehow can stop the actual ffmpeg stream/pid, which keep running in the background. </p>