How to wait for the completion of the first team?

I am writing a script in bash that calls bash internal scripts. Where the first script includes various tests that run in the background and the second script prints the results of the first script.

When I run these two scripts one after another, sometimes the second script is executed before the first script that prints the wrong results ends.

I run both scripts with the original command. Any best deals?

source ../../st_new.sh -basedir $STRESS_PATH -instances $INSTANCES 
source ../../results.sh
+4
source share
3 answers

Shell scripts, regardless of how they are executed, execute one command after another. Thus, your code will execute results.shafter the last command completes st_new.sh.

, : &

cmd &

: " cmd . script."

, & , cmd . , st_new.sh ​​. , script:

cmd &
BACK_PID=$!

(PID) BACK_PID. , :

while kill -0 $BACK_PID ; do
    echo "Process is still active..."
    sleep 1
    # You can add a timeout here if you want
done

, , - /

wait $BACK_PID

, , &. , PID , shell &, PID.

+5

, st_new.sh - , (, touch/tmp/st_new.tmp st_new.sh).
. , , , . -

max_retry=20
retry=0
sleep 10 # Minimum time for st_new.sh to finish
while [ ${retry} -lt ${max_retry} ]; do
   if [ -f /tmp/st_new.tmp ]; then
      break # call results.sh outside loop
   else
      (( retry = retry + 1 ))
      sleep 1
   fi
done
if [ -f /tmp/st_new.tmp ]; then
   source ../../results.sh 
   rm -f /tmp/st_new.tmp
else
   echo Something wrong with st_new.sh
fi
+1

sleep source, script .

But, not understanding what he is doing st_new.sh, we cannot explain to you why there is a certain racing condition.

source ../../st_new.sh -basedir $STRESS_PATH -instances $INSTANCES 
sleep 5 # Wait 5 seconds for st_new.sh to finish.
source ../../results.sh
-2
source

Source: https://habr.com/ru/post/1584834/


All Articles