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:
#!/bin/bash
for ((i = 0; i < ${count}; i++))
Do
Commands1
Done
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:
#!/bin/bash
for ((i = 0; i < ${count}; i++))
Do
{
Commands1
}&
Done
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:
#!/bin/bash
for ((i = 0; i < ${count}; i++))
Do
{
Commands1
}&
Done
Wait
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