执行多个shell脚本同时 [英] Execute multiple shell scripts concurrently

查看:298
本文介绍了执行多个shell脚本同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做以下几件事:搜索结果


  • 执行多个shell脚本(这里是2脚本)兼任。


  • 等待两个脚本完成


  • 转储每个脚本的返回值


然而, main.sh 不能按预期工作。

结果

main.sh

 #!/斌/庆典RET1 =`/ a.sh`和放大器。
RET2 =`/ b.sh`如果[$ {} RET1=-a$ {} RET2=];然后
   睡眠1
其他
   回声$ {} RET1,$ {} RET2
结束

a.sh

 #!/斌/庆典
睡眠10
回音1

b.sh

 #!/斌/庆典
睡眠5
回音2


解决方案

如果您有GNU并行 HTTP://www.gnu .ORG /软件/并口/ 安装,你可以这样做:

 平行-j0'{};回声$? ::: a.sh b.sh

我要退出code,检查其中的一个失败的怀疑,那你其实并不关心什么precise退出code了。在这种情况下,你可以这样做:

 平行-j0 ::: a.sh b.sh ||呼应一方或双方的失败

如果它足以获得失败的最后的误差code:

 平行-j0 --halt 1 ::: a.sh b.sh;回声$?

也许你想干掉a.sh如果b.sh年初完成,但失败:

 平行-j0 --halt 2 ::: a.sh b.sh;回声$?

您可以安装GNU并行只需:

  wget的http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
行chmod 755并行
CP并行SEM

留意GNU并行,以了解更多的介绍视频:
<一href=\"https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1\">https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

I want to do the following things:

  • Execute multiple shell scripts (here 2 scripts) concurrently.

  • Wait until both scripts finish

  • Dump return value of each script

However, main.sh does not work as expected.


main.sh

#!/bin/bash

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

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

a.sh

#!/bin/bash
sleep 10
echo 1

b.sh

#!/bin/bash
sleep 5
echo 2

解决方案

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

parallel -j0 '{}; echo $?' ::: a.sh b.sh

I have a suspicion that you want the exit code to check if one of them failed, and that you actually do not care what the precise exit code was. In that case you can do:

parallel -j0 ::: a.sh b.sh || echo one or both of them failed

If it is sufficient to get the error code of the last that failed:

parallel -j0 --halt 1 ::: a.sh b.sh; echo $?

Maybe you would like to kill a.sh if b.sh finishes early but fails:

parallel -j0 --halt 2 ::: a.sh b.sh; echo $?

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

这篇关于执行多个shell脚本同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆