Multi-thread Linux Shell script and linuxshell
In bash, we run (&) in the background to implement multithreading.
for((i=1;i<=pairs;i++)) do mongo="mongo_00$i" acmeair_web="acmeair_web_00$i"{ docker run --name $mongo -d -p $port_m:27017 mymongodb docker run -d -p $port:9080 --name $acmeair_web --link ${mongo}:mongo acmeair/web } & ((port++)) ((port_m++)) done wait
We have added {} & so that the execution in {} can be performed in the background. The above is equivalent to executing pairs at the same time, and wait is waiting for the execution to complete.
We are not able to control the number of concurrent jobs. We can control the number of concurrent jobs as follows.
Tmp_export ofile = "/tmp/$. fifo "mkfifo $ tmp_kerberofile # create a file of the fifo type exec 6 <> $ tmp_incluofile # point fd6 to the fifo type rm $ tmp_incluofilethread = 15 # Number of threads defined here for (I = 0; I <$ thread; I ++); doechodone> & 6 # In fact, $ thread carriage returns for (I = 0; I <50; I ++); do # loop 50 times read-u6 # One read-u6 command is executed once, then a carriage return is subtracted from fd6, then run down # When there is no carriage return in fd6, stop here to implement thread count control {docker run... mongodb docker run... acmeair/web} & {echo "subprocess is finished"} | {echo "error"} echo> & 6 # When the process ends, add a carriage return to fd6, that is, the read-u6 minus one is added.} & donewait # Wait for all the later child processes to end exec 6> &-# disable fd6exit 0
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.