Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何并发运行,但如果有失败,则退出1?_Bash - Fatal编程技术网

Bash 如何并发运行,但如果有失败,则退出1?

Bash 如何并发运行,但如果有失败,则退出1?,bash,Bash,我有以下sh文件: #!/usr/bin/env bash ecs deploy [some not relevant stuff here] & \ ecs deploy [some not relevant stuff here] & \ ecs deploy [some not relevant stuff here] & \ ecs deploy [some not relevant stuff here] & \ ecs deploy [some not

我有以下sh文件:

#!/usr/bin/env bash
ecs deploy [some not relevant stuff here] & \
ecs deploy [some not relevant stuff here] & \
ecs deploy [some not relevant stuff here] & \
ecs deploy [some not relevant stuff here] & \
ecs deploy [some not relevant stuff here] & \
ecs deploy [some not relevant stuff here]
这在一定程度上起作用,因为它确实同时运行部署命令,但如果任何部署的退出代码大于0,我必须返回相应的退出代码

有没有办法用bash来完成这个任务?如果可能,不使用任何tmp文件,只使用变量

# note normal single quotes in following line
CMD='curl -I shoudfail.com'

# run the command, and log it IF IT FAILS
( $CMD || echo $CMD >> tmpout ) &

# rinse, lather, repeat
CMD='curl -I somecmdthatwillfail.com'

# run the command, and log it IF IT FAILS
( $CMD || echo $CMD >> tmpout ) &

...

while [ ! -z `jobs` ]  
do
   sleep 5
done

如果在代码中使用readline来填充CMD而不是文本,那么可以使用一次迭代的输出作为下一次迭代的输入。

不清楚为什么要使用最大的退出代码;如果没有进一步的信息,我假设您只是想返回一个非零的退出代码,如果其他任何进程都有一个非零的退出代码

#!/usr/bin/env bash
# This array will store process ID's of background processes
declare -a pid

# Run curl and save add its pid to array
curl -I shoudfail.com; echo $? >> tmpout &
pid+=( $! )

# Run another curl and save add its pid to array
curl -I somecmdthatwillfail.com a; echo $? >> tmpout & \
pid+=( $! )

# Run a third curl and save add its pid to array
curl -I google.com; echo $? >> tmpout;
pid+=( $! )

# Wait for all background processes to stop
wait

# Loop through array of pids
for proc in "${pid[@]}"; do
    # Get return value of process from wait
    wait "$proc"
    retval=$?

    # Replace maxret if the return value is larger
    [ "$retval" -gt "$maxret" ] && maxret="$retval"

done

echo "$maxret"
ecs deploy [some not relevant stuff here] & pids+=($!)
ecs deploy [some not relevant stuff here] & pids+=($!)
ecs deploy [some not relevant stuff here] & pids+=($!)
ecs deploy [some not relevant stuff here] & pids+=($!)
ecs deploy [some not relevant stuff here] & pids+=($!)
ecs deploy [some not relevant stuff here] & pids+=($!)

rv=0
for pid in "${pids[@]}"; do
  wait "$pid" || rv=1
done
exit "$rv"

首先,在管理子流程方面,我建议切换到python,因为它有一些模块比使用bash编写的任何程序都要好

这里有很多不错的答案,但我会这样做

#!/bin/bash

sleep 4; /bin/false &
pids+=("$!")
sleep 5 &
pids+=("$!")
sleep 5 &
pids+=("$!")
sleep 5 &
pids+=("$!")
sleep 5 &
pids+=("$!")

declare -A -g exit_tracker=()

rc=0
for pid in ${pids[@]}; do
    wait $pid
    if (( $? == 1 )); then
        exit_tracker[$pid]="FAIL"
    else
        exit_tracker[$pid]="SUCCESS"
    fi
done

for key in ${!exit_tracker[@]}; do
    printf "%s\n" "$key PID STATUS: ${exit_tracker[$key]}"
done
如果你想变得更疯狂您可以使用关联数组命名每个返回代码,并确定哪个pid失败。你可以在你看到sleep\u fail、sleep\u pass\u 1等的地方补充你自己的名字

#!/bin/bash

declare -A -g pid_names=()

sleep 4; /bin/false &
pid_names["sleep_fail"]="$!"
sleep 5 &
pid_names["sleep_pass_1"]="$!"
sleep 5 &
pid_names["sleep_pass_2"]="$!"
sleep 5 &
pid_names["sleep_pass_3"]="$!"
sleep 5 &
pid_names["sleep_pass_4"]="$!"

declare -A -g exit_tracker=()

rc=0
for pid in ${!pid_names[@]}; do
    wait ${pid_names[$pid]}
    if (( $? == 1 )); then
        exit_tracker[$pid]="FAIL"
    else
        exit_tracker[$pid]="SUCCESS"
    fi
done

for key in ${!exit_tracker[@]}; do
    printf "%s\n" "$key PID STATUS: ${exit_tracker[$key]}"
done
上面的输出如下所示:

dumbledore@ansible1a [OPS]:~ > bash test.sh
sleep_fail PID STATUS: FAIL
sleep_pass_4 PID STATUS: SUCCESS
sleep_pass_2 PID STATUS: SUCCESS
sleep_pass_3 PID STATUS: SUCCESS
sleep_pass_1 PID STATUS: SUCCESS

我想,
等待
和你的while循环一样,我错了吗?你能让它在没有
cat
ting文件的情况下工作吗?顺便说一句,我对cmd输出不感兴趣,只对退出代码感兴趣。可能会有多件事情失败-大概你希望能够区分哪些事情有效,哪些没有-在上面的例子中,我只记录失败的事情。将返回代码添加到输出中非常简单。是的,我不需要最大的。。我只想知道非零的,谢谢。我选择了另一个答案,因为它是最简单的一个。我真的尽量避免使用bash,当有复杂的事情发生时更是如此。我通常会为这类东西选择节点。我没有在这个特殊情况下使用node,因为它是一个python项目。当然!只是给你一些选择:)。如果归结为需要知道哪个特定命令返回了非零的出口,这个答案将很容易为您做到这一点。祝你好运