并联使用xargs的运行程序 [英] Running programs in parallel using xargs

查看:102
本文介绍了并联使用xargs的运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有当前的脚本。

I currently have the current script.

#!/bin/bash
# script.sh

for i in {0..99}; do
   script-to-run.sh input/ output/ $i
done

我要在并行使用xargs的运行它。我曾尝试

I wish to run it in parallel using xargs. I have tried

script.sh | xargs -P8

但这样做只是在当时执行一次以上。与-N 8没有运气也是如此。
添加和放大器;在该行的结束在脚本的循环中执行会尝试一次运行该脚本99倍。我如何在一次执行循环仅有8,最多100个。

But doing the above only executed once at the time. No luck with -n8 as well. Adding & at the end of the line to be executed in the script for loop would try to run the script 99 times at once. How do I execute the loop only 8 at the time, up to 100 total.

推荐答案

的xargs 手册页:

本手册页文档的xargs GNU版本。 xargs的读取项目
   从标准输入,由空格分隔(可被保护
   双或单引号或反斜线)或换行符,并执行
   命令(默认为/槽/回波)与任何初始一次或多次
   论点其次是物品从标准输入读取。空白行上
   标准输入被忽略。

This manual page documents the GNU version of xargs. xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial- arguments followed by items read from standard input. Blank lines on the standard input are ignored.

这意味着对于您的例子的xargs 正在等待并收集所有的输出从您的脚本,然后运行回声<即输出> 。不完全是那么有用,也没有你想要的。

Which means that for your example xargs is waiting and collecting all of the output from your script and then running echo <that output>. Not exactly all that useful nor what you wanted.

-n 参数是如何从输入的许多项目与每个被运行(没有,其本身有关并行此处)命令使用。

The -n argument is how many items from the input to use with each command that gets run (nothing, by itself, about parallelism here).

要你想要做什么用的xargs 你需要做更多的东西像这样(未经):

To do what you want with xargs you would need to do something more like this (untested):

printf %s\\n {0..99} | xargs -n 1 -P 8 script-to-run.sh input/ output/

这打破了这个样子。

Which breaks down like this.


  • 的printf%s的\\\\ N {0..99} - 打印一个数字每行从 0 99

  • 运行的xargs

    • 最多每运行命令行一个参数

    • 和运行的最高在一个时间八道工序

    • printf %s\\n {0..99} - Print one number per-line from 0 to 99.
    • Run xargs
      • taking at most one argument per run command line
      • and run up to eight processes at a time

      这篇关于并联使用xargs的运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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