title: How to coordinate two linux processes to run in a two step lockstep schedule?
body: <p>I am testing some reliability scenarios and I would like how write a script or a small application to realize a running schedule for 2 alternating (live/dead) processes like:</p>
  
  <pre><code>First 30s: ProcessA - start, run 
  Next  30s: ProcessB - start, run
  Next  30s: ProcessA - kill -9, Process B run
  Next  30s, ProcessB - kill -9, Process A start, run
  Next  30s, ProcessB - start, run, Process A, run
  </code></pre>
  
  <p>Etc.</p>
  
  <p>With the condition that the schedule never has processes killed (or has the intervals in which both are killed overlap). <strong>We can ignore failures</strong> and assume for the purpose of this script the machine/machines it will run on will not fail and the processes will not fail outside the simulated kill -9 in the script.</p>
  
  <p>So I need some sort of barrier-based coordination... but don't know how to realize it in bash.</p>
  
  <p>This is what I am trying to do, but needless to say it does not work. Upon running the script instantiates $PROGRAM1, $PROGRAM2, and $PROGRAM3 (examples are just for intuition) and then gets stuck forever. <strong>It does not alternate</strong> kill -9 and restarting them in this 2-step lockstep. In other words <strong>the loop does not loop.</strong></p>
  
  <pre><code>  PROGRAM1 = "echo P1; sleep 100"
    PROGRAM2 = "echo P2; sleep 100"
    PROGRAM3 = "echo P3; sleep 600"
  
    $PROGRAM1 &amp;
    echo Started $PROGRAM1
    PID1=$!
    $PROGRAM2 &amp;
    echo Started $PROGRAM2
    PID2=$!
    $PROGRAM3 &amp;
    echo Started $PROGRAM3
  
    while 1; do
        sleep 10
        echo Killing $PROGRAM1
        kill -9 $PID1
        wait $PID1
        sleep 10
        echo Starting $PROGRAM1
        $PROGRAM1 &amp;
        PID1=$!
        sleep 10
        echo Killing $PROGRAM2
        kill -9 $PID2
        wait $PID2
        sleep 10
        echo Starting $PROGRAM2
        $PROGRAM2 &amp;
        PID2=$!
    done
  </code></pre>
  
  <p>$PROGRAM3 is one that monitors the two.</p>