我想在 bash 中同时运行两个命令,等待两个命令都成功,然后再运行第三个命令。此外,我想将其包含到 time
命令中。示意性地:
time { { cmd_1 & cmd_2 & } && cmd 3; } &> time.log
例如,如果我运行:
time { { echo "hi" & sleep 5 & } && echo "ok"; } &> time.log
我立即打印出“ok”,无需等待大括号之间的命令成功。此外,time.log
文件在括号中包含两个命令的标准错误和标准输出,但 time
的输出仅打印在终端中,而不是文件中.
我确信我遗漏了一些简单明了的东西!
最佳答案
像这样:
time (
( sleep 5; exit 1 ) & pid1=$! # process returns 1 and causes nok
( sleep 6; exit 0 ) & pid2=$!
wait $pid1 && wait $pid2 && echo aok || echo nok # wait for both and act accordingly
) 2> time_out # time outputs to std_err
输出
nok
因为第一个 bg 进程返回 1。
关于bash - 当一组并行命令成功时如何运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523861/