Bash technique: execute multiple commands concurrently in the script and wait for the command execution to finish


In Bash, you can use control operators&Let the command run in the background, and then use thewaitThe built-in command waits for the task to complete.

Control operator&

See man bash for control operators&The description of is as follows:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell.
The shell does not wait for the command to finish, and the return status is 0.

That is, when the command to be executed&At the end, the command is executed in the backend shell.
The current shell will not wait for this command to finish executing. You can continue to execute the next command.

That is, when a command takes a long time to execute, if the&At the end, the current shell waits for the command to complete before it can execute the next command.
While taking&At the end, the command is placed in the back sub shell to execute, and the current shell can continue to execute the next command.

Wait built in command

See help wait for the description of the command as follows:

wait: wait [-n] [id …]
Wait for job completion and return exit status.
Waits for each process identified by an ID, which may be a process ID or a job specification, and reports its termination status.
If ID is not given, waits for all currently active child processes, and the return status is zero.
If ID is a a job specification, waits for all processes in that job’s pipeline.

If the -n option is supplied, waits for the next job to terminate and returns its exit status.

That is,waitThe command can wait for the process execution of the specified PID to complete.
If no parameters are provided, wait for all currently activated subprocesses to complete execution.

When there are multiple time-consuming operations that can be executed concurrently, and these operations are all executed, and then the next operation is carried out, you can use thewaitCommand to wait for these operations to complete.

Similar to the following statement:

command1 &
command2 &

command1 &Command use&Specify execution in the backgroundcommand1Command.
If executedcommand1Command takes a long time, do not add&We need to waitcommand1Execution is complete before the next command can be executed.
Added&After, execute in the backgroundcommand1Command to continue with the next command.

Allied,command2 &Also in the backgroundcommand2Command.

That is, through&Concurrent execution in the backgroundcommand1command2Command can make better use of CPU concurrency and speed up execution.
If you wait firstcommand1Execution is complete, then executioncommand2Command, may be slow.

After executionwaitCommand, which does not provide any parameters, will wait for all the activated subprocesses to finish executing. The subprocesses executed in the background are also in the active state.
We’ll wait herecommand1command2All completed.

Therecommand1command2It’s just the name of the example. In the actual test, you need to change it to the executable command.

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]