In Bash, you can use control operators
&Let the command run in the background, and then use the
waitThe built-in command waits for the task to complete.
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.
&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.
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 the
waitCommand to wait for these operations to complete.
Similar to the following statement:
command1 & command2 & wait
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.
&After, execute in the backgroundcommand1Command to continue with the next command.
command2 &Also in the backgroundcommand2Command.
That is, through
&Concurrent execution in the backgroundcommand1、command2Command can make better use of CPU concurrency and speed up execution.
If you wait firstcommand1Execution is complete, then executioncommand2Command, may be slow.
waitCommand, 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 herecommand1、command2All completed.
Therecommand1、command2It’s just the name of the example. In the actual test, you need to change it to the executable command.