sabinz / DCO-Modelling-of-Deep-Time-Atmospheric-Carbon-Flux-from-Subduction-Zone-Interactions

GNU Lesser General Public License v3.0
1 stars 2 forks source link

Use `wait` to wait for background jobs to finish #1

Open LukeMondy opened 1 year ago

LukeMondy commented 1 year ago

Hey guy

https://github.com/sabinz/DCO-Modelling-of-Deep-Time-Atmospheric-Carbon-Flux-from-Subduction-Zone-Interactions/blob/4ebbba10f86c843657b9b53f75615da672aeb277/SubductionZone_ContinentalArc_Length_AlternativeWorkflow/STEP1-LaunchBatchContinentalGridding.sh#L45

You can use the wait command to get bash to wait until all the background commands are done, like in the "Multiple Processes wait Example" in this page: https://phoenixnap.com/kb/bash-wait-command

It would mean the whole script will exit when all the background jobs are done (though you would also need to delete the nohup too ;))

sabinz commented 1 year ago

Good tip! Can it also preserve the Terminal Output into a file? This currently seems to work OK, but I'll keep an eye out on whether the wait command makes things better. :-)

LukeMondy commented 1 year ago

Yeah, should be able to do

proc=0
while (($proc <= ($nproc-1)))

do

    age_max=$(echo "$age1-($proc*$step)" | bc -l)
    age_min=$(echo "$age_max-($step-1)" | bc -l)

    if [ $age_min -lt $age2 ];
    then
        age_min=$age2
    fi  

    mkdir CONTINENTAL_GRIDS_${age_min}_${age_max}   

    cp ${script} CONTINENTAL_GRIDS_${age_min}_${age_max}
    cp reconstruct_features_v2.py CONTINENTAL_GRIDS_${age_min}_${age_max}
    cp continents.cpt CONTINENTAL_GRIDS_${age_min}_${age_max}
    cp $rotation_file CONTINENTAL_GRIDS_${age_min}_${age_max}/CombinedRotations.rot
    cp $continental_geometries CONTINENTAL_GRIDS_${age_min}_${age_max}/ContinentalTerranes.gpml

    cd CONTINENTAL_GRIDS_${age_min}_${age_max}/ 

    ./${script} ${age_min} ${age_max} > stdout_${age_max}-${age_min} &   # no nohup used
    echo "Continental gridding job for ages " ${age_min} " to " ${age_max} " launched!"
    cd ..   

proc=$(($proc + 1))
done    

wait # wait for all background tasks to finish.