繁体   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