title: Wait command not acting as expected in Bash Script
body: <p>I am making a script to play 4 different video feeds from 4 different camera DVRs via RTSP, and need them to be pretty much hands-free i.e. a dedicated mini pc running Debian that will, network connection permitting, always have the feeds playing without users needing to restart anything. My script below works as expected at first, but if I close one of the feeds or disconnect then reconnect the network connection, the next (and all the following) iterations of the <code>while</code> loop this happens:</p>
  
  <ol>
  <li>feeds 1 through 4 connect correctly</li>
  <li><code>wmctrl</code> places windows correctly</li>
  <li>All windows immediately close, and the loop continues as if the connection was lost.    </li>
  </ol>
  
  <p>I've tried removing the process numbers from the <code>wait</code> command as well as moving the function declaration outside of the loop (not sure what help that would do but tried nonetheless) but that changed nothing.  </p>
  
  <pre><code>#!/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" &gt; /dev/null 2&gt;&amp;1 &amp; p1=$!
      ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source2 -vf "scale=w=960:h=540" &gt; /dev/null 2&gt;&amp;1 &amp; p2=$!
      ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source3 -vf "scale=w=960:h=540" &gt; /dev/null 2&gt;&amp;1 &amp; p3=$!
      ffplay -rtsp_transport tcp -infbuf -stimeout 100 -autoexit -i $source4 -vf "scale=w=960:h=540" &gt; /dev/null 2&gt;&amp;1 &amp; 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 ] &amp;&amp; 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 &gt; /dev/null 2&gt;&amp;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
  </code></pre>
  
  <p>I need it to behave like it does the first iteration and wait until something actually goes wrong. I assume it has something to do with the <code>wait</code> command and processes or job control or something but google searching has led me nowhere. Can someone help me please?</p>