簡體   English   中英

Bash:在Shell腳本中使用進程ID

[英]Bash: Using process IDs in a shell script

我對PID和父/子進程的使用感到有些困惑。 我一直在閱讀它們,並且我了解一個事實,即程序啟動時會對其自身(孩子)進行精確復制,並且每個人都有唯一的PID。 但是我不確定是否可以在shell中使用它來告訴我該shell程序的某些方面何時完成。

舉一個更好的例子(偽代碼):

 for ((i = 0; i < 10; i++))
  for a_name in "${anArray[@]}";do
     a series of math equations that allow the values associated with a_name to run in the background simultaneously using '&' earlier in the code         
  done
wait

  for a_name in "${anArray[@]}";do
     same as above but diff equations
  done
wait
done

我希望能夠看到命令中任何給定的值何時完成,以便該特定值可以移至下一個for循環和命令。

我已經看到,wait可以將作業標識符作為參數(wait%1或wait $ PPID),但是我不確定如何實現這些標識符。

有沒有人對如何使用PID提出建議和/或提供超好教程的鏈接? (我的意思是超級好,我需要在這里加上一些外行的條件)

謝謝!

您可以等待一個過程:

command ${array[0]} &
waiton=$!
# Do some more stuff which might finish before process $waiton
wait $waiton

您可以等待所有子進程:

someLongRunningCommand &
(
    for value in "${array[@]}"; do
        command "$value" &
    done
    wait
)
# wait with no arguments waits on all children processes of the current
# process. That doesn't include `someLongRunningCommand`, as it is not
# a child of the process running the subshell.

其他情況則比較棘手,可以通過xargsparallel或其他方法更好地處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM