问题描述
我有一个关于在shell脚本并行处理的问题。我有一个程序我的
,我希望运行多次,在一个循环中的一个循环。这个程序基本上是这样的:
程序
I have a question about parallel processing in shell scripting. I have a program myProgram
, which I wish to run multiple times, in a loop within a loop. This program is basically this:
MYPATHDIR=`ls $MYPATH`
for SUBDIRS in $MYPATHDIR; do
SUBDIR_FILES=`ls $MYPATH/$SUBDIRS`
for SUBSUBDIRS in $SUBDIR_FILES; do
find $MYPATH/$SUBDIRS/$SUBSUBDIRS | ./myProgram $MYPATH/$SUBDIRS/outputfile.dat
done
done
我希望做的是采取并行处理的优势。所以,我想这对于中间线来启动所有的 myPrograms
一次:
(find $MYPATH/$SUBDIRS/$SUBSUBDIRS | ./myProgram $MYPATH/$SUBDIRS/outputfile.dat &)
然而,这一切始于300左右调用 myProgram
同时,造成RAM问题等。
However, this began all 300 or so calls to myProgram
simultaneously, causing RAM issues etc.
我想这样做是运行在平行内环 myProgram
的每一次出现,但在移动到下一个之前等待所有这些完成外循环迭代。基于所述解答this问题,我试过如下:
What I would like to do is to run each occurrence of myProgram
in the inner loop in parallel, but wait for all of these to finish before moving on to the next outer loop iteration. Based on the answers to this question, I tried the following:
for SUBDIRS in $MYPATHDIR; do
SUBDIR_FILES=`ls $MYPATH/$SUBDIRS`
for SUBSUBDIRS in $SUBDIR_FILES; do
(find $MYPATH/$SUBDIRS/$SUBSUBDIRS | ./myProgram $MYPATH/$SUBDIRS/outputfile.dat &)
done
wait $(pgrep myProgram)
done
但我得到以下警告/错误,重复多次:
But I got the following warning/error, repeated multiple times:
./myScript.sh: line 30: wait: pid 1133 is not a child of this shell
......,所有的 myPrograms
开始了一次,和以前一样。
...and all the myPrograms
were started at once, as before.
我是什么做错了吗?我能做些什么来实现我的目标是什么?谢谢你。
What am I doing wrong? What can I do to achieve my aims? Thanks.
推荐答案
()
调用子shell,然后调用查找/ myprogram,让你处理孙子的进程。你不能等待孙子,只有直系后裔(又名儿童)。
()
invokes a subshell, which then invokes find/myprogram, so you're dealing with "grandchildren" processes. You can't wait on grandchildren, only direct descendants (aka children).
这篇关于在shell脚本并行处理,'pid是不是这个壳的孩子“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!