AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1257199
Accepted
An Ignorant Wanderer
An Ignorant Wanderer
Asked: 2020-07-08 14:11:24 +0800 CST2020-07-08 14:11:24 +0800 CST 2020-07-08 14:11:24 +0800 CST

如何批量运行命令?

  • 772

我有以下脚本:

command file1 &
command file2 &
command file3 &
command file4 &
command file5 &
command file6 &
# SOME OTHER COMMAND 
command file7 &
command file8 &
command file9 &
command file10 &
command file11 &
command file12 &

在“其他命令”的位置,我想要一个命令来停止执行下一个命令,直到它之前的所有进程都完成,以便我有效地“批量”运行命令。我怎样才能做到这一点?

bash process
  • 3 3 个回答
  • 1556 Views

3 个回答

  • Voted
  1. Best Answer
    steeldriver
    2020-07-08T14:41:15+08:002020-07-08T14:41:15+08:00

    您应该能够使用 bash shell 的内置wait命令。来自man bash(强调我的):

           wait [-n] [n ...]
                  Wait for each specified child process and return its termination
                  status.  Each n may be a process ID or a job specification; if a
                  job  spec  is  given,  all  processes in that job's pipeline are
                  waited for.  If n is not given, all currently active child  pro‐
                  cesses are waited for, and the return status is zero.  If the -n
                  option is supplied, wait waits for  any  job  to  terminate  and
                  returns  its exit status.  If n specifies a non-existent process
                  or job, the return status is 127.  Otherwise, the return  status
                  is the exit status of the last process or job waited for.

    所以

    command file1 &
    command file2 &
    command file3 &
    command file4 &
    command file5 &
    command file6 &
    
    wait
    
    command file7 &
    command file8 &
    command file9 &
    command file10 &
    command file11 &
    command file12 &
    
    • 2
  2. glenn jackman
    2020-07-08T14:43:11+08:002020-07-08T14:43:11+08:00

    如果你想有点花哨,一个 bash 脚本

    batch() {
        for arg; do
            theCommand "$arg" &
        done
        wait
    }
    
    batch file{1..6}
    batch file{7..12}
    
    • 2
  3. ɲeuroburɳ
    2020-07-09T12:56:01+08:002020-07-09T12:56:01+08:00

    xargs当您使用不同的参数运行相同的命令时,这是一个很好的选择。

    echo file{1..32} | xargs -P 32 -n 1 command
    

    -P 32告诉 xargs 一次最多运行 32 个进程。该-n 1方法最多将一个参数传递给命令(没有它,它可能会将所有文件发送到单个命令,因此不会并行执行)。

    因此,在这种情况下,请设置-P为您的批量大小。另一个选项(和好处)是您可以设置-P您想要实际使用的并行度。虽然该&方法将为每个命令启动一个进程,但 xargs 会将进程数限制为您设置的数量。这意味着您可以将并行度定位到您拥有的内核数量,或者使用较小的数量来防止大批量进程使您的机器陷入爬行状态。

    哦,当然,command用你的实际命令替换。

    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve