如何将 GNU 并行与 awssync 命令结合使用?
我有一个包含以下命令的文件:
aws s3 cp ./test s3://test --recursive --content-encoding "gzip" --content-type "text/html" --cache-control "max-age=$MAXAGE" --exclude "*" --include "*.html" --profile $PROFILE
aws s3 cp ./test s3://test $S3BUCKET --recursive --content-encoding "gzip" --content-type "text/html" --cache-control "max-age=$MAXAGE" --exclude "*" --include "*.css" --profile $PROFILE
如何使用 GNU Parallel 并行运行这些命令?
我所做的是将命令添加到名为 test.sh 的文件中
我运行以下命令
parallel < test.sh
] 如何将参数传递给 test.sh 文件?例如,我想传入aws存储桶名称。
最佳答案
如果您的目标是在一组手写命令的任何成员失败时触发脚本失败,那么 GNU 并行并不是完成这项工作的最佳工具:shell 本身已经提供了 wait 所需的一切
命令,which is specified by POSIX and present out-of-the-box on all standards-compliant shells (另请参阅规范requiring it to be implemented as a builtin)。
#!/bin/bash
# ^^^^- Important! /bin/sh doesn't have arrays; bash, ksh, or zsh will work.
# For readability, put common arguments in an array
common_args=(
--recursive
--content-encoding "gzip"
--content-type "text/html"
--cache-control "max-age=$MAXAGE"
--exclude "*"
--profile "$PROFILE"
)
# Record PIDs of the various jobs in an array
pids=( )
aws s3 cp ./test s3://test --include='*.html' "${common_args[@]}" & pids+=( $! )
aws s3 cp ./test s3://test "$S3BUCKET" --include='*.css' "${common_args[@]}" & pids+=( $! )
# If either background job failed, exit the script with the same exit status
for pid in "${pids[@]}"; do
wait "$pid" || exit
done
注意,上面使用数组是为了方便,而不是必须;如果您的目标是编写可在任何 POSIX 基线 shell 上运行的代码,您可以使用函数提供通用参数,和/或在标量变量中构建 PID 数组,或者通过覆盖 shell 函数内的“$@”。
关于bash - 如何使用 GNU 并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33743471/