linux shell 同时执行,bash – 同时执行多个shell脚本

这是我一直在运行的一些代码,它们似乎完全符合您的要求.只需在适当的位置插入./a.sh和./b.sh:

# Start the processes in parallel...

./script1.sh 1>/dev/null 2>&1 &

pid1=$!

./script2.sh 1>/dev/null 2>&1 &

pid2=$!

./script3.sh 1>/dev/null 2>&1 &

pid3=$!

./script4.sh 1>/dev/null 2>&1 &

pid4=$!

# Wait for processes to finish...

echo -ne "Commands sent... "

wait $pid1

err1=$?

wait $pid2

err2=$?

wait $pid3

err3=$?

wait $pid4

err4=$?

# Do something useful with the return codes...

if [ $err1 -eq 0 -a $err2 -eq 0 -a $err3 -eq 0 -a $err4 -eq 0 ]

then

echo "pass"

else

echo "fail"

fi

请注意,这会捕获脚本的退出状态,而不是它输出到stdout的内容.没有简单的方法来捕获在后台运行的脚本的标准输出,因此我建议您使用exit status将信息返回给调用进程.

你可能感兴趣的:(linux,shell,同时执行)