gpt4 book ai didi

linux - xargs 可以用于并行运行多个任意命令吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:39 27 4
gpt4 key购买 nike

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

我可以在仅改变参数时使用 xargs -P。当我想要改变我遇到困难的可执行文件和参数时。

例子:命令列表.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 parallel 但目前只能使用 xargs。我也不能只将所有命令设置为后台,因为主机可能一次处理的命令太多。

解决方案可能就在眼前。提前致谢!

最佳答案

如果您无法访问并行,一种解决方案是使用 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)并搜索参数(在本例中为 COMMAND)并将其替换为 xargs 正在处理的当前行。手册页信息如下:

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

关于linux - xargs 可以用于并行运行多个任意命令吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16942868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com