带参数执行

execution with argument

我正在使用下面的 xargs 命令在 file.txt 包含主机的所有主机上并行执行(不带参数)。

xar-n 1 -P 500 SCRIPT_PATHt

下面是在一台主机上执行脚本(带参数)的方法

SCRIPT_PATH host1 

现在我的要求是通过 xarg 命令并行地在多个主机上执行带有参数的脚本。 在下面尝试过,但没有用。是否可以通过 xargs 完成?

xargs -n 1 -P 500 SCRIPT_PATH "

xargs 解决方案:-I 参数

xargs 有一个 -I option:

The xargs command offers options to insert the listed arguments at some position other than the end of the command line. The -I option to xargs takes a string that will be replaced with the supplied input before the command is executed. A common choice is %.

你的情况:

xargs -n 1 -P 500 -I % SCRIPT_PATH % "arguments" < file.txt # % as input line
xargs -n 1 -P 500 -I {} SCRIPT_PATH {} "arguments" < file.txt # {} as input line

GNU并行解决方案

GNU Parallel - 与 xargs 一样,但更强大和方便。

parallel - build and execute shell command lines from standard input in parallel

管道输入 parallel

您的脚本可以与 parallel 一起使用,就像 xargs:

parallel -n 1 -P 500 -I {} SCRIPT_PATH {} "arguments" < file.txt # just replace xargs with parallel
parallel -P 500 SCRIPT_PATH {} "arguments" < file.txt # you can omit -n 1 -I arg

parallel 使用 --argfile 参数

您可以将输入文件 file.txt 设置为参数,而不是通过管道传输其内容。如果您想将 STDIN 脚本用于其他目的,这可能会有所帮助。

parallel -P 500 --arg-file file.txt SCRIPT_PATH {} "arguments"  # --arg-file argument
parallel -P 500 -a file.txt SCRIPT_PATH {} "arguments"  # -a is an --arg-file synonym
parallel -P 500 SCRIPT_PATH {} "arguments" :::: file.txt # '::::' - another way to write -a argfile1

parallel 使用 --sshloginfile

远程执行脚本

您可以使用 --sshloginfile-slf

直接从 parallel 管理远程主机上的脚本执行
parallel -P 500 --slf file.txt --nonall SCRIPT_PATH "arguments"