同时执行多个shell脚本
我想做以下事情:
同时执行多个 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
a.sh
#!/bin/bash
sleep 10
echo 1
b.sh
#!/bin/bash
sleep 5
echo 2
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有 GNU Parallel http://www.gnu.org/software/parallel/安装后你可以这样做:
我怀疑你想要退出代码来检查其中一个是否失败,并且你实际上并不关心确切的退出代码是什么。在这种情况下,您可以这样做:
如果足以获取最后一个失败的错误代码:
如果 b.sh 提前完成但失败,也许您想杀死 a.sh:
您可以简单地通过以下方式安装 GNU Parallel:
观看GNU Parallel 的介绍视频以了解更多信息:
https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
打印作弊码工作表: https://www.gnu.org/software/parallel/parallel_cheat.pdf
If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:
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:
If it is sufficient to get the error code of the last that failed:
Maybe you would like to kill a.sh if b.sh finishes early but fails:
You can install GNU Parallel simply by:
Watch the intro videos for GNU Parallel to learn more:
https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
Print the cheat sheet: https://www.gnu.org/software/parallel/parallel_cheat.pdf
这是我一直在运行的一些代码,它似乎完全符合您的要求。只需在适当的地方插入
./a.sh
和./b.sh
:请注意,这捕获脚本的退出状态,而不是它输出到
stdout 的内容
。没有简单的方法来捕获在后台运行的脚本的stdout
,因此我建议您使用 退出状态,将信息返回给调用进程。Here is some code that I have been running, that seems to do exactly what you want. Just insert
./a.sh
and./b.sh
where appropriate:Note that this captures the exit status of the script and not what it outputs to
stdout
. There is no easy way of capturing thestdout
of a script running in the background, so I would advise you to use the exit status to return information to the calling process.您寻求的答案就在这个问题 shell - 获取后台进程的退出代码
基本上,当您将进程置于后台时,您无法直接获取其退出代码。但如果运行 bash wait 命令,那么
wait
的退出代码将返回后台进程的退出代码。即使 a.sh 在运行 wait 之前结束,这也会起作用。特殊变量
$?
保存前一个进程的退出代码。$!
保存先前运行进程的进程 ID。The answer you seek is in this question shell - get exit code of background process
Basically, when you background a process you can't get its exit code directly. But if you run the bash wait command, then
wait
's exit code will return the exit code of the background process.This will work even if a.sh ends before you run wait. The special variable
$?
holds the exit code of the previous process. And$!
holds the Process ID of the previously run process.如果您有 bash 4.2 或更高版本,以下内容可能对您有用。它使用关联数组来存储任务名称及其“代码”以及任务名称及其 pid。我还内置了一个简单的速率限制方法,如果您的任务消耗大量 CPU 或 I/O 时间并且您想要限制并发任务的数量,该方法可能会派上用场。
该脚本在第一个循环中启动所有任务,并在第二个循环中使用结果。
对于简单的情况来说,这有点矫枉过正,但它允许非常简洁的东西。例如,可以将每个任务的错误消息存储在另一个关联数组中,并在一切稳定下来后打印它们。
(我已从我的答案此处复制了这个答案,因为它解决了这两个问题,如果不行,请告诉我或替换直接通过链接或任何合适的方式即可。)
If you have bash 4.2 or later available the following might be useful to you. It uses associative arrays to store task names and their "code" as well as task names and their pids. I have also built in a simple rate-limiting method which might come handy if your tasks consume a lot of CPU or I/O time and you want to limit the number of concurrent tasks.
The script launches all tasks in the first loop and consumes the results in the second one.
This is a bit overkill for simple cases but it allows for pretty neat stuff. For example one can store error messages for each task in another associative array and print them after everything has settled down.
(I have copied this answer over from my answer here because it solves both questions, if that's not ok please tell me or replace it directly with just a link or whatever is suitable.)
反引号不给出命令返回的值,而是给出命令的输出。要获取返回值:
如果您弄错了问题并且实际上想要命令的输出,您也会得到它,因为它们都会转到脚本的标准输出。
Backticks do not give the value returned by the command, but the output of the command. To get the return values:
If you mistated the question and do in fact want the output of the commands, you get that as well since they will both go to the stdout of the script.