如果 TOTAL CPU 小于 1%,则使用 monit 重启 ffmpeg 进程

Restart ffmpeg process using monit if TOTAL CPU is less than 1%

我用过类似这样的解决方案Restarting ffmpeg process using monit 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 Restarting ffmpeg process using monit,我将在下面解释。所以这是我的监视器配置:

    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

这是我的 streambash.sh 文件:

    #!/bin/bash
    pid_file="PATH-to-file/streampid.pid"

    case "" 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" &> /dev/null &
        ch_pid=$! 
        echo "Start Stream1: ffmpeg = $ch_pid";
        echo $ch_pid > $pid_file
           ;;

     stop)
        echo "Stop ffmpeg Stream1";
        kill `cat $pid_file` &> /dev/null
           ;;

     *)
        echo "Usage: PATH-to-file/streambash.sh {start|stop|restart}"
        exit 1
           ;;

     esac
    exit 0
    echo $pid_file

Monit 可以成功启动 bash 文件,但是当在 monit 配置中匹配此条件 "if TOTAL CPU is less than 1% for 10 cycles then restart" 时,它会尝试重新启动,但会报错该过程不是 运行。但实际上 ffmpeg 进程仍在后台运行,我可以看到流在我的网站上是实时的。这是监控日志:

    [CET Jan 10 12:55:02] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:07] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:12] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:17] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:22] error    : 'FFMPEGStream' total cpu usage of 0.9% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:27] error    : 'FFMPEGStream' total cpu usage of 0.9% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:32] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:37] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:42] error    : 'FFMPEGStream' total cpu usage of 0.0% matches resource limit [cpu usage>1.0%]
    [CET Jan 10 12:55:47] error    : 'FFMPEGStream' total cpu usage of 0.4% matches resource limit [cpu usage>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

Monit 不断尝试重新启动进程,并且在每次重试时,它都会将一个新的 pid 转储到 PATH-to-file/streampid.pid,但正如我所说,它似乎可以以某种方式停止实际的 ffmpeg stream/pid,它在后台保留 运行。

您的轮询周期/守护进程检查间隔很短,只有 5 秒?

FFMpeg 未在 5 秒内启动,因此 monit 尝试再次启动它,不断循环。

如果你想有这么低的检查间隔,你需要在启动命令上设置一个超时,按照以下行:

start program = "PATH-to-file/streambash.sh restart" with timeout 30 seconds

这确实有助于我理解 monit 的思维方式,在 monit 做事的同时在终端 window 中观看实时日志:

 tail -f /var/log/monit.log