加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 同时执行多个shell脚本

发布时间:2020-12-15 19:00:20 所属栏目:安全 来源:网络整理
导读:我想做以下事情: 同时执行多个shell脚本(此处为2个脚本). 等到两个脚本完成 转储每个脚本的返回值 但是,main.sh无法按预期工作. main.sh #!/bin/bashret1=`./a.sh` ret2=`./b.sh`if [ "${ret1}"="" -a "${ret2}"="" ]; then sleep 1else echo ${ret1},${ret
我想做以下事情:

>同时执行多个shell脚本(此处为2个脚本).
>等到两个脚本完成
>转储每个脚本的返回值

但是,main.sh无法按预期工作.

main.sh

#!/bin/bash

ret1=`./a.sh` &
ret2=`./b.sh`

if [ "${ret1}"="" -a "${ret2}"="" ]; then
   sleep 1
else
   echo ${ret1},${ret2}
end

#!/bin/bash
sleep 10
echo 1

b.sh

#!/bin/bash
sleep 5
echo 2
这是我一直在运行的一些代码,它们似乎完全符合您的要求.只需在适当的位置插入./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将信息返回给调用进程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读