首页 > 解决方案 > 如何为这个嵌套的 Bash 脚本使用超时?

问题描述

我编写了以下 bash 脚本,除了一些随机的时刻,它完全冻结并且不会进一步超过某个值,它工作正常a0

export OMP_NUM_THREADS=4

N_SIM=15000
N_NODE=1

for ((i = 1; i <= $N_SIM; i++))
do
    index=$((i))
    a0=$(awk "NR==${index} { print \$2 }" Intensity_Wcm2_versus_a0_10_20_10_25_range.txt)
    dirname="a0_${a0}"
    if [ -d "${dirname}" ]; then
        cd -P -- "${dirname}" # enter the directory because it exists already
        if [ -f "ParticleBinning0.h5" ]; then # move to next directory because the sim has been already done and results are there
            cd ..
            echo ${a0}
            echo We move to the next directory because ParticleBinning0.h exists in this one already.
            continue 1
        else
            awk -v s="a0=${a0}" 'NR==6 {print s} 1 {print}' ../namelist_for_smilei.py > namelist_for_smilei_a0included.py
            echo ${a0}
            mpirun -n 1 ../smilei namelist_for_smilei_a0included.py 2&> smilei.log
            cd ..
        fi
    else
        mkdir -p $dirname
        cd $dirname
        awk -v s="a0=${a0}" 'NR==6 {print s} 1 {print}' ../namelist_for_smilei.py > namelist_for_smilei_a0included.py
        echo ${a0}
        mpirun -n 1 ../smilei namelist_for_smilei_a0included.py 2&> smilei.log 
        cd ..
    fi
done

我需要让它运行 12 个小时左右才能完成所有 15,000 次模拟。

一个mpirun -n 1 ../smilei namelist_for_smilei.py 2&> smilei.log命令平均需要 4 秒才能运行。

有时它只是停在一个值上,屏幕上a0最后一个打印的值是 say 。它保持这样,保持这样,无缘无故。从那个特别有问题的文件夹中没有写入任何输出。所以我不知道这个特殊值发生了什么。的任何值都不是特别重要,我可以不用计算 1 的特定值。a0a0_12.032131smilei.loga0_12.032131a0a0a0

我试图在 Ubuntu 中使用 timeout 实用程序以某种方式使其超过任何需要 2 分钟以上才能运行的 a0 值。如果运行时间超过此时间,它显然会失败并停止整个进程向前运行。

编写这样的脚本超出了我的能力范围。

我的特定管道的模板应该如何?

谢谢!

标签: bashterminalparallel-processingtimeoutmpi

解决方案


看来这个mpirun程序挂了。正如您所说,您可以timeout在经过合理的时间后使用该实用程序终止其执行:

timeout --signal INT 2m mpirun...

根据mpirun处理信号的方式,可能需要使用 KILL 而不是 INT 来终止进程。


推荐阅读