Hi I want to use the wait in multiple command line but it seems that it's not working:
sleep 10 &
[1] 13004
(date; wait; sleep 15; date) &
31 oct 2021 18:34:32 -05
31 oct 2021 18:34:47 -05
Look that in the second date it should display 18:34:57 because the wait supposes to finish 10 seconds after the first sleep.
Is there something that I'm missing ?
Thanks for your help
CodePudding user response:
From linux.die.net
wait [n ...]
Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.
The key is that it waits for child processes. Using parentheses creates a new sub-shell, which means that the stuff inside of the parentheses will not be a "parent" of the things that were run in the background before it.
CodePudding user response:
Oh great, thanks for your answer. So this is how it works!!
sleep 5 & date; wait; sleep 5 && date &
[1] 19031
31 oct 2021 19:51:10 -05
[1] Done sleep 5
[1] 19034
31 oct 2021 19:51:20 -05
Just wondering how can put it all command in background process
Thanks