[英]Run script command on parallel
i've bash script which I need to run on it two command in parallel 我有bash脚本,我需要在它上面并行运行两个命令
For example I'm executing a command of npm install which takes some time (20 -50 secs) 例如,我正在执行npm install命令,这需要一些时间(20-50秒)
and I run it on two different folders in sequence first npm install on books folder and the second is for orders folder, is there a way to run both in parallel in shell script ? 然后我按顺序在两个不同的文件夹上运行它首先在books文件夹上安装npm,第二个用于orders文件夹,有没有办法在shell脚本中并行运行?
For example assume the script is like following: 例如,假设脚本如下所示:
#!/usr/bin/env bash
dir=$(pwd)
cd $tmpDir/books/
npm install
grunt
npm prune production
cd $tmpDir/orders/
npm install
grunt
npm prune production
You could use &
to run the process in the background, for example: 您可以使用
&
在后台运行该过程,例如:
#!/bin/sh
cd $HOME/project/books/
npm install &
cd $HOME/project/orders/
npm install &
# if want to wait for the processes to finish
wait
To run and wait for nested/multiple processes you could use a subshell ()
for example: 要运行并等待嵌套/多个进程,您可以使用subshell
()
,例如:
#!/bin/sh
(sleep 10 && echo 10 && sleep 1 && echo 1) &
cd $HOME/project/books/
(npm install && grunt && npm prune production ) &
cd $HOME/project/orders/
(npm install && grunt && npm prune production ) &
# waiting ...
wait
In this case, notice the that the commands are within ()
and using &&
that means that only the right side will be evaluated if the left size succeeds (exit 0) so for the example: 在这种情况下,请注意命令在
()
并且使用&&
这意味着如果左侧大小成功(退出0),则仅评估右侧,因此对于示例:
(sleep 10 && echo 10 && sleep 1 && echo 1) &
()
()
之间 sleep 10
and if succeeds &&
then runs echo 10
, if succeeds &&
then run sleep 1
and if succeeds &&
then runs echo 1
sleep 10
,如果成功&&
然后运行echo 10
,如果成功&&
然后运行sleep 1
并且如果成功&&
然后运行echo 1
&
&
结束命令在后台运行所有这些
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.