同时执行多个shell脚本

编程入门 行业动态 更新时间:2024-10-26 00:30:39
本文介绍了同时执行多个shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想做以下事情:

I want to do the following things:

  • 同时执行多个 shell 脚本(这里是 2 个脚本).

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

等待两个脚本完成

转储每个脚本的返回值

但是,main.sh 没有按预期工作.

However, main.sh does not work as expected.

#!/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

推荐答案

如果你有 GNU Parallel www.gnu/software/parallel/ 安装你可以这样做:

If you have GNU Parallel www.gnu/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 $?

如果 b.sh 提前完成但失败,您可能想杀死 a.sh:

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

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

您可以简单地通过以下方式安装 GNU Parallel:

You can install GNU Parallel simply by:

$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || fetch -o - pi.dk/3 ) > install.sh $ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a 12345678 883c667e 01eed62f 975ad28b 6d50e22a $ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0 cc21b4c9 43fd03e9 3ae1ae49 e28573c0 $ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf 79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224 fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35 $ bash install.sh

观看 GNU Parallel 的介绍视频以了解更多信息:www.youtube/playlist?list=PL284C9FF2488BC6D1

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

打印备忘单:www.gnu/software/parallel/parallel_cheat.pdf

更多推荐

同时执行多个shell脚本

本文发布于:2023-06-06 09:45:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   脚本   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!