concurrency in shell scripts

Source: Internet
Author: User

The main record is the concurrency and serial execution of commands in the shell script.

By default, the commands in the shell script are executed serially, and must wait until the previous command is finished before executing the next command, but if I have a large number of commands that need to be executed and have no effect on each other (which is more complicated) then use the concurrency of the commands.


Look at the following code:

    1. #!/bin/bash

    2. for ((i = 0; i < ${count}; i++))

    3. Do

    4. Commands1

    5. Done

    6. Commands2


For the above code, because each COMMANDS1 is time-consuming, it is intended to use concurrent programming, which can save a lot of time.


The modified code is as follows:


    1. #!/bin/bash

    2. for ((i = 0; i < ${count}; i++))

    3. Do

    4. {

    5. Commands1

    6. }&

    7. Done

    8. Commands2


In this case, COMMANDS1 can be executed in parallel. The essence is to execute the COMMANDS1 as a background process so that the main process does not have to wait for the previous command to complete before executing the next command.

But my original goal is to let the commands1 cycle be executed, and then use Command2 to deal with the previous results. If it is written like this, the COMMANDS1 has already begun to execute COMMANDS2 when it is not finished, and the result has been wrong.

Modify the code again as follows:

    1. #!/bin/bash

    2. for ((i = 0; i < ${count}; i++))

    3. Do

    4. {

    5. Commands1

    6. }&

    7. Done

    8. Wait

    9. Commands2


Above this can achieve the intended purpose, first all the commands1 in the background in parallel execution, wait until the loop inside the command is finished before the next commands2 execution.


For the above code, if the count value is particularly large, we should control the number of concurrent processes, otherwise it will affect the operation of other processes of the system, or even panic.



This article is from the "Linux Oracle MariaDB" blog, so be sure to keep this source http://wangergui.blog.51cto.com/8504247/1926470

concurrency in shell scripts

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.