【问题标题】:How to invoke while statement multiple times in parallel using xargs max-procs utility如何使用 xargs max-procs 实用程序并行多次调用 while 语句
【发布时间】:2017-12-18 14:31:56
【问题描述】:

我在 Linux 中有一个名为 test.txt 的文件。内容如下。

test.txt

['table1']
['table2']
['table3']
['table4']
['table5']
and so on

现在我有一个循环 test.txt 文件的 while 语句可以完成一些工作。

While 声明如下:

while read -r line; do
  table=${line:2:-2}
  validateTable=$(hive --database history -e "SHOW TABLES LIKE '$table'")
  if [[ -z $validateTable ]]; then
      /home/"$USER"/import.py "${table}"
  else
       /home/"$USER"/append.py "${table}"
  fi
done < < test.txt

在这个while 语句中validateTable 是检查表是否存在于hive 中。

如果不存在,那么它将调用import.py 脚本

如果存在,它将调用append.py 脚本。

现在 while 语句可以正常工作了。我得到了预期的结果。

要求:

我需要的是我想并行调用while statement。我的意思是我希望while 同时运行 10 次。

什么是最好的解决方案。

我发现我们可以使用xargs --max-procs 选项来做到这一点,但不知道如何使用它。

【问题讨论】:

    标签: linux bash while-loop do-while xargs


    【解决方案1】:

    xargs 需要一个命令才能运行,这意味着您需要将循环体放入 shell 脚本中。类似的东西(叫它myscript

    #!/bin/bash
    table=${1:2:-2}
    validateTable=$(hive --database history -e "SHOW TABLES LIKE '$table'")
    if [[ -z $validateTable ]]; then
      /home/"$USER"/import.py "${table}"
    else
      /home/"$USER"/append.py "${table}"
    fi
    

    然后运行类似的东西

    xargs --max-procs 10 myscript < test.txt
    

    【讨论】:

    • 我已经试过你的代码了。它仅适用于一张桌子并退出。它不适用于所有表,即使我使用 xargs --max-porcs 10
    • 我通过在 xargs 语句中添加 -n 1 来使其工作,例如 xargs --max-procs 10 -n 1 myscript &lt; test.txt
    猜你喜欢
    • 2017-12-17
    • 2015-04-06
    • 2012-04-07
    • 1970-01-01
    • 2022-08-22
    • 2016-06-07
    • 1970-01-01
    • 2020-11-28
    • 2018-02-19
    相关资源
    最近更新 更多