【问题标题】:Restart ffmpeg process using monit if TOTAL CPU is less than 1%如果 TOTAL CPU 小于 1%,则使用 monit 重新启动 ffmpeg 进程
【发布时间】:2017-01-10 12:14:16
【问题描述】:

我使用了类似Restarting ffmpeg process using monit 的解决方案来重新启动我的 ffmpeg 流,以防它因某种原因失败。请记住它不是重复的问题/问题,因为我还有其他问题与示例问题/解决方案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 "$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" &> /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 文件,但是当这个条件 "if TOTAL CPU is less than 1% for 10 cycles then restart" 在 monit 配置中匹配时,它会尝试重新启动,它给出进程未运行的错误。但实际上 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 流/pid,一直在后台运行。

【问题讨论】:

  • 为什么不用 nice 或 cgroups?
  • 我的问题不是 CPU 处理,我的问题是进程没有重新启动,或者如果在它没有运行之后至少没有显示为在 monit 中启动。 CPU使用率只是我以前知道进程不工作或ffmpeg正在编码或任何相关问题的一种方式。所以使用 nice 或 cgroups 没有意义。

标签: linux shell ffmpeg monitoring monit


【解决方案1】:

您的轮询周期/守护程序检查间隔非常低,为 5 秒?

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

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

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

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

 tail -f /var/log/monit.log

【讨论】:

  • 另一件事,monit / monit webif 在最近启动的进程中检测和更新 cpu 使用率非常慢。请耐心等待一分钟或更长时间!即使进程启动并使用了例如 10% 的 CPU,monit webif 指出进程仍在使用 0% 并重新启动进程,有时会出现永无止境的循环。
最近更新 更多