Multi-thread Linux Shell script and linuxshell

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.