![백그라운드에서 백그라운드 작업을 기다리는 방법은 무엇입니까?](https://linux55.com/image/98705/%EB%B0%B1%EA%B7%B8%EB%9D%BC%EC%9A%B4%EB%93%9C%EC%97%90%EC%84%9C%20%EB%B0%B1%EA%B7%B8%EB%9D%BC%EC%9A%B4%EB%93%9C%20%EC%9E%91%EC%97%85%EC%9D%84%20%EA%B8%B0%EB%8B%A4%EB%A6%AC%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
다음과 같은 질문이 있습니다.
$ some_command & # Adds a new job as a background process
$ wait && echo Foo # Blocks until some_command is finished
$ wait && echo Foo & # Is started as a background job and is done immediately
내가 하고 싶은 것은 wait &
백그라운드에서 기다리는 것입니다.기타 모든 백그라운드 작업완성된.
이것을 달성할 수 있는 방법이 있나요?
답변1
어떤 시점에서는 명령이 실행될 때까지 기다려야 합니다.
그러나 몇 가지 기능에 명령을 넣을 수 있다면 필요한 작업을 수행하도록 예약할 수 있습니다.
some_command(){
sleep 3
echo "I am done $SECONDS"
}
other_commands(){
# A list of all the other commands that need to be executed
sleep 5
echo "I have finished my tasks $SECONDS"
}
some_command & # First command as background job.
SECONDS=0 # Count from here the seconds.
bg_pid=$! # store the background job pid.
echo "one $!" # Print that number.
other_commands & # Start other commands immediately.
wait $bg_pid && echo "Foo $SECONDS" # Waits for some_command to finish.
wait # Wait for all other background jobs.
echo "end of it all $SECONDS" # all background jobs have ended.
절전 시간이 코드 3 및 5에 표시된 경우 some_command는 나머지 작업보다 먼저 종료되고 실행될 때 인쇄됩니다.
one 760
I am done 3
Foo 3
I have finished my tasks 5
end of it all 5
예를 들어 수면 시간이 8과 5인 경우 다음이 인쇄됩니다.
one 766
I have finished my tasks 5
I am done 8
Foo 8
end of it all 8
순서에 주의하세요. 실제로 모든 섹션은 가능한 한 가깝습니다( $SECONDS
인쇄물의 가치).