unix脚本中的某些命令不起作用



我有invoke_test.sh,代码如下:

echo "invoke script started"
sh test1.sh >output1.log &
sh test2.sh > output2.log
echo "test 1 completed " >> output1.log
echo "test 2 completed " >> output2.log

test1.sh:

echo "running test1.sh"
sleep 5
echo "test1.sh completed"

test2.sh:

echo "running test2.sh"
sleep 10
echo "test2.sh completed"

所以当我执行invoke_test.sh时sh invoke_test.sh

我可以找到iN输出日志文件,例如output1.log

运行test1.shtest1.sh完成

但无法从invoke_test.sh脚本中获取我试图直接记录的以下行测试1完成

这里的要求是并行运行两个shell脚本,一旦两个脚本都执行完毕,我想执行这两行回声;测试1完成">gt;输出.log回声;测试2完成">gt;output2.log

我是unix的新手,所以我会理解为什么这种行为以一种简单的方式发生,以及我如何实现所需的输出

如果您的目标是wait,直到完成所有后台进程,请使用wait:

echo "invoke script started"
sh test1.sh > output1.log &
sh test2.sh > output2.log &
wait   # wait for both of the background jobs to complete
echo "test 1 completed " >> output1.log
echo "test 2 completed " >> output2.log

最新更新