wait कमांड
In this page:
command &
pid=$!
wait "$pid"
wait
Waiting for a Specific Job
wait pid script को उस particular background process के खत्म होने तक block करता है, और फिर wait खुद उस process का exit status return करता है, जो $? में available हो जाता है।
उदाहरण: Waiting for a Specific Job
#!/bin/bash
(exit 3) &
pid=$!
wait "$pid"
echo "background job exited with status $?"
Login to try C/C++/Java/PHP code in the editor
Waiting for All Background Jobs
बिना किसी arguments के wait call करना तब तक block करता है जब तक current shell द्वारा शुरू किया गया हर background job पूरा न हो जाए, चाहे उनकी संख्या कुछ भी हो।
उदाहरण: Waiting for All Background Jobs
#!/bin/bash
for i in 1 2 3; do
( sleep 0.05; echo "job $i done" ) &
done
wait
echo "all jobs finished"
Login to try C/C++/Java/PHP code in the editor
Collecting Multiple Exit Statuses
कई background jobs के individual result check करने के लिए, हर PID save करें और उन पर एक-एक करके wait करें, क्योंकि बिना arguments वाला एक bare wait सिर्फ यह बताता है कि सब कुछ खत्म हो गया, हर job का individual outcome नहीं।
उदाहरण: Collecting Multiple Exit Statuses
#!/bin/bash
(exit 0) &
pid_ok=$!
(exit 1) &
pid_bad=$!
wait "$pid_ok"
echo "first job status: $?"
wait "$pid_bad"
echo "second job status: $?"
Login to try C/C++/Java/PHP code in the editor
- बिना arguments के
waitcall करना और यह मान लेना कि यह सिर्फ last job के लिए wait करता है; बिना arguments के यह सभी currently running background jobs के लिए wait करता है। - यह भूल जाना कि
wait $pidउस job का असली exit status return करता है, जिसे तुरंत capture न करने पर track करना आसान नहीं रहता। - एक ऐसे PID पर
waitकरने की कोशिश करना जो current shell का direct child नहीं था (उदाहरण के लिए, एक पूरी तरह अलग process का PID), जो fail होता है क्योंकिwaitसिर्फ shell के अपने child jobs पर काम करता है।
- बिना arguments के
waitतब तक block करता है जब तक current shell द्वारा शुरू किया गया हर background job खत्म न हो जाए। wait pidएक specific job के लिए wait करता है और इसका exit statuswaitके अपने exit status के रूप में return करता है।waitसिर्फ उन processes पर उपयोग किया जा सकता है जो current shell के direct children हैं।wait pidके ठीक बाद$?capture करना आपको backgrounded command का असली exit code देता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: