This question already has an answer here:
这个问题在这里已有答案:
- Bash script processing limited number of commands in parallel 4 answers
- Bash脚本处理有限数量的命令并行4个答案
Im working on large project and we have multiple npm packages
.
我在大型项目上工作,我们有多个npm包。
I want to install all the packages in parallel which mean that i want all of to run on the same time (to save time) and once the last install has finished to continue with my script.
我想并行安装所有软件包,这意味着我希望所有软件包同时运行(以节省时间),并且在最后一次安装完成后继续我的脚本。
Example script:
#!/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
In not going to accept the fastest answer but the one who will do what i wish to do and do not have the right knowledge on how to do it, so you can tale the time and give a full answer.
不接受最快的答案,但是那个会做我想做的事情并且没有正确的知识如何去做的人,所以你可以记住时间并给出完整的答案。
Thank you very much in advance.
非常感谢你提前。
1 个解决方案
#1
4
Execute npm install
in the background with:
在后台执行npm install:
npm install &
Then after the done
line you can wait for all the background processes to finish with:
然后在完成行之后,您可以等待所有后台进程完成:
wait
This command is explained in the bash manual
:
bash手册中解释了此命令:
Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero.
等到每个进程ID pid或作业规范jobspec指定的子进程退出并返回等待的最后一个命令的退出状态。如果给出了作业规范,则等待作业中的所有进程。如果未给出参数,则等待所有当前活动的子进程,并且返回状态为零。
#1
4
Execute npm install
in the background with:
在后台执行npm install:
npm install &
Then after the done
line you can wait for all the background processes to finish with:
然后在完成行之后,您可以等待所有后台进程完成:
wait
This command is explained in the bash manual
:
bash手册中解释了此命令:
Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero.
等到每个进程ID pid或作业规范jobspec指定的子进程退出并返回等待的最后一个命令的退出状态。如果给出了作业规范,则等待作业中的所有进程。如果未给出参数,则等待所有当前活动的子进程,并且返回状态为零。