如何在bash中等待直到壳脚本完成?
现在,我正在将此脚本用于程序:
export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh
cd /home/ubuntu/fastsurfer
datadir=/home/ubuntu/moya/data
fastsurferdir=/home/ubuntu/moya/output
mkdir -p $fastsurferdir/logs # create log dir for storing nohup output log (optional)
while read p ; do
echo $p
nohup ./run_fastsurfer.sh --t1 $datadir/$p/orig.nii \
--parallel --threads 16 --sid $p --sd $fastsurferdir > $fastsurferdir/logs/out-${p}.log &
sleep 3600s
done < /home/ubuntu/moya/data/subjects-list.txt
而不是使用Sleep 3600s
,因为该程序需要大约一个小时,我想使用等待
直到所有进程(几个PID)完成。 如果这是正确的方法,您能告诉我如何做吗?
Br Alex
right now I'm using this script for a program:
export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh
cd /home/ubuntu/fastsurfer
datadir=/home/ubuntu/moya/data
fastsurferdir=/home/ubuntu/moya/output
mkdir -p $fastsurferdir/logs # create log dir for storing nohup output log (optional)
while read p ; do
echo $p
nohup ./run_fastsurfer.sh --t1 $datadir/$p/orig.nii \
--parallel --threads 16 --sid $p --sd $fastsurferdir > $fastsurferdir/logs/out-${p}.log &
sleep 3600s
done < /home/ubuntu/moya/data/subjects-list.txt
Instead of using sleep 3600s
, as the program needs around an hour, I'd like to use wait
until all processes (several PIDS) are finished.
If this is the right way, can you tell me how to do that?
BR Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
等待
将等待所有背景过程完成(请参阅帮助等待
)。因此,您需要的只是在创建所有背景过程之后运行等待
。wait
will wait for all background processes to finish (seehelp wait
). So all you need is to runwait
after creating all of the background processes.这可能比您要求的要多,但我认为我将提供一些控制您要一次运行的线程数量的方法。我发现我总是想限制数字,原因各种原因。
解释
以下将限制并发线程一次运行。我还使用主设计模式,因此我们有一个主体,可以使用函数run_jobs运行脚本,该函数可以处理调用和等待。我将所有$ p的所有P读为一个数组,然后在启动线程时穿越该数组。它要么将启动一个长达4个线程,要么等待5秒,一旦至少一个小于4,它将启动另一个线程。完成后,等待剩下的任何要做。如果您想要更简单的事情,我也可以做到这一点。
注意:我没有运行此代码,因为您没有提供足够的信息来实际使用。
This may be more than what you are asking for but I figured I would provide some methods for controlling the number of threads you want to have running at once. I find that I always want to limit the number for various reasons.
Explaination
The following will limit concurrent threads to max_threads running at one time. I am also using the main design pattern so we have a main that runs the script with a function run_jobs that handles the calling and waiting. I read all of $p into an array, then traverse that array as we launch threads. It will either launch a thread up to 4 or wait 5 seconds, once there are at least one less than four it will start another thread. When finished it waits for any remaining to be done. If you want something more simplistic I can do that as well.
Note: I did not run this code since you have not provided enough information to actually do so.