如何在bash中等待直到壳脚本完成?

发布于 2025-01-24 02:25:50 字数 715 浏览 1 评论 0原文

现在,我正在将此脚本用于程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2025-01-31 02:25:50

等待将等待所有背景过程完成(请参阅帮助等待)。因此,您需要的只是在创建所有背景过程之后运行等待

wait will wait for all background processes to finish (see help wait). So all you need is to run wait after creating all of the background processes.

抚笙 2025-01-31 02:25:50

这可能比您要求的要多,但我认为我将提供一些控制您要一次运行的线程数量的方法。我发现我总是想限制数字,原因各种原因。

解释

以下将限制并发线程一次运行。我还使用主设计模式,因此我们有一个主体,可以使用函数run_jobs运行脚本,该函数可以处理调用和等待。我将所有$ p的所有P读为一个数组,然后在启动线程时穿越该数组。它要么将启动一个长达4个线程,要么等待5秒,一旦至少一个小于4,它将启动另一个线程。完成后,等待剩下的任何要做。如果您想要更简单的事情,我也可以做到这一点。

#!/usr/bin/env bash

export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh

typeset max_threads=4
typeset subjects_list="/home/ubuntu/moya/data/subjects-list.txt"
typeset subjectsArray

run_jobs() {
    local child="$"
    local num_children=0
    local i=0
    while [[ 1 ]] ; do
        num_children=$(ps --no-headers -o pid --ppid=$child | wc -w) ; ((num_children-=1))
        echo "Children: $num_children"
        if [[ ${num_children} -lt ${max_threads} ]] ;then
            if [ $i -lt ${#subjectsArray[@]} ] ;then
                ((i+=1))
                # RUN COMMAND HERE &
                ./run_fastsurfer.sh --t1 $datadir/${subjectsArray[$i]}/orig.nii \
                                --parallel --threads 16 --sid ${subjectsArray[$i]} --sd $fastsurferdir 
            fi
        fi
        sleep 10
    done
    wait
}


main() {
    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)

    mapfile -t subjectsArray < ${subjects_list}
    run_jobs
}

main

注意:我没有运行此代码,因为您没有提供足够的信息来实际使用。

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.

#!/usr/bin/env bash

export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh

typeset max_threads=4
typeset subjects_list="/home/ubuntu/moya/data/subjects-list.txt"
typeset subjectsArray

run_jobs() {
    local child="$"
    local num_children=0
    local i=0
    while [[ 1 ]] ; do
        num_children=$(ps --no-headers -o pid --ppid=$child | wc -w) ; ((num_children-=1))
        echo "Children: $num_children"
        if [[ ${num_children} -lt ${max_threads} ]] ;then
            if [ $i -lt ${#subjectsArray[@]} ] ;then
                ((i+=1))
                # RUN COMMAND HERE &
                ./run_fastsurfer.sh --t1 $datadir/${subjectsArray[$i]}/orig.nii \
                                --parallel --threads 16 --sid ${subjectsArray[$i]} --sd $fastsurferdir 
            fi
        fi
        sleep 10
    done
    wait
}


main() {
    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)

    mapfile -t subjectsArray < ${subjects_list}
    run_jobs
}

main

Note: I did not run this code since you have not provided enough information to actually do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文