且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行多个shell脚本同时

更新时间:2023-12-05 12:07:16

如果您有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并行,以了解更多的介绍视频:
https://www.***.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.***.com/playlist?list=PL284C9FF2488BC6D1