简体   繁体   English

如何等待第一个命令完成?

[英]how to wait for first command to finish?

I am writing a script in bash, which is calling two bash scripts internally. 我正在用bash编写一个脚本,它在内部调用两个bash脚本。 Where first script includes different tests which runs in background and second script print results of first script. 第一个脚本包括在后台运行的不同测试和第二个脚本打印第一个脚本的结果。

When I run these two scripts one after other, sometimes, second script get executed before first script ends Which prints wrong results. 当我一个接一个地运行这两个脚本时,有时,第二个脚本会在第一个脚本结束之前执行,这会打印出错误的结果。

I am running both scripts with source command. 我正在使用source命令运行这两个脚本。 Any better suggestions? 有更好的建议吗?

source ../../st_new.sh -basedir $STRESS_PATH -instances $INSTANCES 
source ../../results.sh

Shell scripts, no matter how they are executed, execute one command after the other. Shell脚本,无论它们如何执行,都会在另一个之后执行一个命令。 So your code will execute results.sh after the last command of st_new.sh has finished. 因此,在st_new.sh的最后一个命令完成后,您的代码将执行results.sh

Now there is a special command which messes this up: & 现在有一个特殊的命令搞砸了这个: &

cmd &

means: "Start a new background process and execute cmd in it. After starting the background process, immediately continue with the next command in the script." 表示:“启动一个新的后台进程并在其中执行cmd 。启动后台进程后,立即继续执行脚本中的下一个命令。”

That means & doesn't wait for cmd to do it's work. 这意味着&不等待cmd做它的工作。 My guess is that st_new.sh contains such a command. 我的猜测是st_new.sh包含这样的命令。 If that is the case, then you need to modify the script: 如果是这种情况,那么您需要修改脚本:

cmd &
BACK_PID=$!

This puts the process ID (PID) of the new background process in the variable BACK_PID . 这将新后台进程的进程ID(PID)放在变量BACK_PID You can then wait for it to end: 然后,您可以等待它结束:

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

or, if you don't want any special handling/output simply 或者,如果您不想要任何特殊处理/输出

wait $BACK_PID

Note that some programs automatically start a background process when you run them, even if you omit the & . 请注意,某些程序在运行时会自动启动后台进程,即使您省略了& Check the documentation, they often have an option to write their PID to a file or you can run them in the foreground with an option and then use the shell's & command instead to get the PID. 检查文档,他们通常可以选择将PID写入文件,或者您可以使用选项在前台运行它们,然后使用shell的&命令来获取PID。

Make sure that st_new.sh does something at the end what you can recognize (like touch /tmp/st_new.tmp when you remove the file first and always start one instance of st_new.sh). 确保st_new.sh最终能够识别出什么(比如首先删除文件时触摸/tmp/st_new.tmp并始终启动st_new.sh的一个实例)。
Then make a polling loop. 然后进行轮询循环。 First sleep the normal time you think you should wait, and wait short time in every loop. 首先睡觉你认为应该等待的正常时间,并在每个循环中等待很短的时间。 This will result in something like 这会产生类似的结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM