并行运行可指定数量的命令-对比xargs -P,GNU并行和"moreutils"平行线 [英] Run a specifiable number of commands in parallel - contrasting xargs -P, GNU parallel, and "moreutils" parallel

查看:106
本文介绍了并行运行可指定数量的命令-对比xargs -P,GNU并行和"moreutils"平行线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以bash脚本在26台服务器上运行多个mongodump.

I'm trying to run multiple mongodump's on 26 servers in a bash script.

我可以运行3条命令,例如

I can run 3 commands like

mongodump -h staging .... & mongodump -h production .... & mongodump -h web ... &

mongodump -h staging .... & mongodump -h production .... & mongodump -h web ... &

同时,当一个完成时,我想启动另一个mongodump.

at the same time, and when one finishes I want to start another mongodump.

我不能同时运行所有26个mongodumps命令,服务器将在CPU上用尽.同时最多3个mongodumps.

I can't run all 26 mongodumps commands at the same time, the server will run out on CPU. Max 3 mongodumps at the same time.

推荐答案

您可以使用 xarg-P选项并行运行一定数量的调用:

请注意,-P选项为 POSIX未强制要求,但GNU xargs和BSD/macOS xargs都支持.

Note that the -P option is not mandated by POSIX, but both GNU xargs and BSD/macOS xargs support it.

xargs -P 3 -n 1 mongodump -h <<<'staging production web more stuff and so on'

这将并行运行mongodump -h stagingmongodump -h productionmongodump -h web,等待所有3个调用完成,然后继续执行mongodump -h moremongodump -h stuffmongodump -h and,依此类推.

This runs mongodump -h staging, mongodump -h production, and mongodump -h web in parallel, waits for all 3 calls to finish, then continues with mongodump -h more, mongodump -h stuff, and mongodump -h and, and so on.

-n 1从输入流中获取一个单个参数并调用mongodump;根据需要进行调整,必要时在输入中使用单引号或双引号.

-n 1 grabs a single argument from the input stream and calls mongodump; adjust as needed, single- or double-quoting arguments in the input if necessary.

注意: GNU xargs-但不支持BSD xargs-支持-P 0,其中0表示:同时运行尽可能多的进程."

Note: GNU xargs - but not BSD xargs - supports -P 0, where 0 means: "run as many processes as possible simultaneously."

默认情况下,通过stdin提供的参数被追加到指定命令.
如果您需要控制 where ,则将相应的参数放在结果命令中,

By default, the arguments supplied via stdin are appended to the specified command.
If you need to control where the respective arguments are placed in the resulting commands,

  • 逐行提供参数
  • 使用-I {}进行指示,并将{}定义为每个输入行的占位符.
  • provide the arguments line by line
  • use -I {} to indicate that, and to define {} as the placeholder for each input line.
xargs -P 3 -I {} mongodump -h {} after <<<$'staging\nproduction\nweb\nmore\nstuff'

现在,每个输入参数都替换为{},从而允许后面添加参数after.

Now each input arguments is substituted for {}, allowing argument after to come after.

但是请注意,每条输入行总是作为单个参数传递.

Note, however, that each input line is invariably passed as a single argument.

BSD/macOS xargs允许您将-n-J {}组合在一起,而无需提供基于行的输入,但是GNU xargs不支持-J.
简而言之:只有BSD/macOS允许您将输入参数的放置与一次读取多个参数结合起来.

BSD/macOS xargs would allow you to combine -n with -J {}, without needing to provide line-based input, but GNU xargs doesn't support -J.
In short: only BSD/macOS allows you to combine placement of the input arguments with reading multiple arguments at once.

请注意,xargs不会并行化命令的标准输出并行输出,以便并行处理的输出可以交错 到达.
使用 GNU parallel可以避免此问题-参见下文.

Note that xargs does not serialize stdout output from commands in parallel, so that output from parallel processes can arrive interleaved.
Use GNU parallel to avoid this problem - see below.

xargs的优势在于它是标准实用程序,因此在支持-P的平台上没有先决条件.

xargs has the advantage of being a standard utility, so on platforms where it supports -P, there are no prerequisites.

在Linux世界中(尽管也通过 Homebrew 在macOS上),有两个专门构建的实用程序,用于在Windows中运行命令并行,不幸的是,它们具有相同的名称; 通常,您必须按需安装它们:

In the Linux world (though also on macOS via Homebrew) there are two purpose-built utilities for running commands in parallel, which, unfortunately, share the same name; typically, you must install them on demand:

    moreutils软件包中的
  • parallel(二进制)-请参见其主页.

  • parallel (a binary) from the moreutils package - see its home page.

功能更强大-来自parallel包的GNU parallel(Perl脚本)谢谢,其主页. /p>

The - much more powerful - GNU parallel (a Perl script) from the parallel package Thanks, twalberg. - see its home page.

如果您已经具有parallel实用程序,则parallel --version会告诉您它是哪个实用程序(GNU parallel报告版本号和版权信息,"moreutils" parallel抱怨无效的选项并显示一个语法摘要).

If you already have a parallel utility, parallel --version will tell you which one it is (GNU parallel reports a version number and copyright information, "moreutils" parallel complains about an invalid option and shows a syntax summary).

parallel -j 3 -n 1 mongodump -h -- staging production web more stuff and so on

# Using -i to control placement of the argument, via {}
# Only *1* argument at at time supported in that case.
parallel -j 3 -i mongodump -h {} after -- staging production web more stuff and so on

xargs不同,此parallel实现不采用从 stdin 传递的参数;所有传递参数必须在--之后在命令行中传递.

Unlike xargs, this parallel implementation doesn't take the arguments to pass through from stdin; all pass-through arguments must be passed on the command line, following --.

据我所知,此parallel实现提供的唯一功能超出了xargs的功能:

From what I can tell, the only features this parallel implementation offers beyond what xargs can do is:

  • -l选项允许延迟进一步的调用,直到系统负载超出指定的阈值以下为止.
  • 可能是这样(来自man页):"stdout和stderr是通过相应的内部管道进行序列化的,以防止烦人的并发输出行为.",尽管我发现这不是 man页的日期为2009-07-2的版本中就是这种情况-请参阅最后一节.
  • The -l option allows delaying further invocations until the system load overage is below the specified threshold.
  • Possibly this (from the man page): "stdout and stderr is serialised through a corresponding internal pipe, in order to prevent annoying concurrent output behaviour.", though I've found this not be the case in the version whose man page is dated 2009-07-2 - see last section.

Ole Tange 求助.

Tip of the hat to Ole Tange for his help.

parallel -P 3 -n 1 mongodump -h <<<$'staging\nproduction\nweb\nmore\nstuff\nand\nso\non'

# Alternative, using ::: followed by the target-command arguments.
parallel -P 3 -n 1 mongodump -h ::: staging production web more stuff and so on 

# Using -n 1 and {} to control placement of the argument.
# Note that using -N rather than -n would allow per-argument placement control
# with {1}, {2}, ...
parallel -P 3 -n 1 mongodump -h {} after <<<$'staging\nproduction\nweb\nmore\nstuff\nand'

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆