bash - 如何使用 GNU 并行

标签 bash parallel-processing gnu gnu-parallel

如何将 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/

相关文章:

git - 如何在bash中获取两个点之间的字符串?

c# - CPU 绑定(bind)任务的并行化继续与 IO 绑定(bind)

parallel-processing - "reduce"函数能否在函数式编程中并行化?

python - scrapy安装错误

bash - 如何判断当前终端 session 是否在 GNU screen 中?

linux - 如何安排在几秒钟内定时重启我的服务器?

linux - 如何在 Linux/Unix 中查找与标识符匹配的字符序列?

windows - 在关机或启动时运行 Cygwin 脚本

c - 获取程序传输的总 MPI 字节数

iphone - 如何在 iPhone 上使用 GNU C 编译器/gcc 和移动终端编译简单的 C 文件?