等待命令未在Bash脚本中按预期方式运行

时间:2019-04-14 04:17:28

标签: bash shell

我正在编写一个脚本,以通过RTSP播放来自4个不同摄像机DVR的4个不同的视频提要,并且需要它们几乎免提,即运行Debian的专用微型PC,只要网络连接允许,它将始终具有提要无需用户重新启动即可播放。首先,下面的脚本可以按预期工作,但是如果我关闭其中一个提要或断开连接,然后重新连接网络连接,则会发生while循环的下一个(以及以下所有)迭代:

  1. 供稿1到4正确连接
  2. wmctrl正确放置窗口
  3. 所有窗口立即关闭,并且循环继续进行,就好像连接丢失了一样。

我已经尝试从wait命令中删除进程号,并将函数声明移到循环外(不知道有什么帮助,但还是尝试了),但没有改变。

#!/bin/bash

case $1 in
        -d)  debug=1    ;;
        *)    ;;
esac

close_all() {
    kill $p1 $p2 $p3 $p4
    return
}

source1="rtsp://user:password@10.0.20.30:554/streaming/channels/001"
source2="rtsp://user:password@10.0.20.33:1026/streaming/channels/001"
source3="rtsp://user:password@10.0.20.31:1024/streaming/channels/001"
source4="rtsp://user:password@10.0.20.34:1027/streaming/channels/001"

while true; do

    ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source1 -vf "scale=w=960:h=540" > /dev/null 2>&1 & p1=$!
    ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source2 -vf "scale=w=960:h=540" > /dev/null 2>&1 & p2=$!
    ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source3 -vf "scale=w=960:h=540" > /dev/null 2>&1 & p3=$!
    ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source4 -vf "scale=w=960:h=540" > /dev/null 2>&1 & p4=$!
    sleep 5

    window1=$(wmctrl -l | grep "$source1" | cut -d' ' -f 1 )
    window2=$(wmctrl -l | grep "$source2" | cut -d' ' -f 1 )
    window3=$(wmctrl -l | grep "$source3" | cut -d' ' -f 1 )
    window4=$(wmctrl -l | grep "$source4" | cut -d' ' -f 1 )

    wmctrl -i -r $window1 -e 0,0,0,960,540
    wmctrl -i -r $window2 -e 0,0,540,960,540
    wmctrl -i -r $window3 -e 0,960,0,960,540
    wmctrl -i -r $window4 -e 0,960,540,960,540

    [ "$debug" = 1 ] && echo " $p1 $p2 $p3 $p4"

    # Originally had wait -n $p1 $p2 $p3 $p4 here 
    wait -n

        #One of the ffplay commands exited so something must be wrong, 
        #close all and retry

    close_all

    echo "Connection interrupted, retrying..."
    echo "Or press q to exit..."
    read -t 3 -N 1 input
    case $input in
        q|Q) exit   ;;
        *)  ;;
    esac

        # Picking an arbitrary DVR to test for now.  
    until ping -q -c 1 10.0.20.30 > /dev/null 2>&1; do
        echo "Connection failed, will keep retrying in 3 seconds..."
        echo "Press q to abort."
        read -t 3 -N 1 input
        case $input in
            q|Q)    exit ;;
            *)  ;;
        esac
    done

done

我需要它表现得像第一次迭代一样,然后等到实际出问题为止。我认为它与wait命令和流程或工作控制有关,但是Google搜索使我无处可去。有人可以帮我吗?

0 个答案:

没有答案