【问题标题】:Can xargs be used to run several arbitrary commands in parallel?xargs 可以用于并行运行多个任意命令吗?
【发布时间】:2013-06-01 07:50:35
【问题描述】:

我希望能够提供一长串任意/不同命令(不同的二进制/可执行文件和参数)并让 xargs 并行运行这些命令 (xargs -P)。

当只改变参数时,我可以很好地使用 xargs -P。当我想改变我遇到困难的可执行文件和参数时。

示例:command-list.txt

% cat command-list.txt
binary_1 arg_A arg_B arg_C 
binary_2 arg_D arg_E
.... <lines deleted for brevity>
binary_100 arg_AAA arg_BBB

% xargs -a command-list.txt -P 4 -L 1
 ** I know the above command will only echo my command-list.txt **

我知道 GNU 并行,但现在只能使用 xargs。我也不能只后台处理所有命令,因为主机可能一次处理太多命令。

解决方案可能正盯着我看。提前致谢!

【问题讨论】:

标签: linux shell command-line parallel-processing xargs


【解决方案1】:

如果您无权访问并行,一种解决方案就是将 sh 与您的命令一起用作参数。

例如:

xargs -a command-list.txt -P 4 -I COMMAND sh -c "COMMAND"

sh 的 -c 基本上只是执行给定的字符串(而不是寻找文件)。手册页的解释是:

-c string   If  the  -c  option  is  present, then commands are read from
            string.  If there are arguments after the  string,  they  are
            assigned to the positional parameters, starting with $0.

xargs 的 -I 告诉它一次运行一个命令(如 -L 1),并用 xargs 正在处理的当前行搜索和替换参数(在本例中为 COMMAND)。手册页信息如下:

-I replace-str
            Replace occurrences of replace-str in the initial-arguments with
            names read from standard input.  Also, unquoted  blanks  do  not
            terminate  input  items;  instead  the  separator is the newline
            character.  Implies -x and -L 1.

sh 似乎对包含引号 (") 的命令非常宽容,因此您似乎不需要将它们正则表达式转换为转义引号。

【讨论】:

    猜你喜欢
    • 2011-10-20
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    相关资源
    最近更新 更多