如何执行并行命令

时间:2015-12-31 23:20:39

标签: node.js bash shell ubuntu zsh

我正在处理大型项目,我们有多个npm packages

我想并行安装所有软件包,这意味着我希望所有软件包同时运行(以节省时间),并且在上次安装完成后继续我的脚本。

示例脚本:

#!/bin/zsh
#...

NPM_FOLDERS=(
    common.library/audit
    common.library/cipher
    common.library/logger
    ...
)

# Get the number of total folders to process
counter=${#NPM_FOLDERS[@]};

# counter for user feedback to the current install folder index
index=1;

# loop and install all the required packages
for folder in $NPM_FOLDERS;
do 
    #  Print the installation message with the folder & couters
    echo "\033[38;5;255m($index/$counter) Executing npm install in: \033[38;5;226m$folder";

    # change the folder to the required location    
    cd $ROOT_FOLDER/$folder;

    # Execute install on this folder
    npm install ;

    # increase current index
    let index++;

done

echo
echo "\033[38;5;11mInstallation completed."
echo 

不接受最快的答案,但是那个会做我想做的事并且没有正确知识的人,所以你可以把时间花在一个完整的答案上。

非常感谢你。

1 个答案:

答案 0 :(得分:4)

在后台执行npm install

npm install &

然后在done行之后,您可以等待所有后台进程完成:

wait

此命令在bash manual

中说明
  

等待每个进程ID pid或作业规范jobspec指定的子进程退出并返回等待的最后一个命令的退出状态。如果给出了作业规范,则等待作业中的所有进程。如果没有给出参数,等待所有当前活动的子进程,返回状态为零。

相关问题