并行化 bash 脚本并等待每个循环完成

Parallelize a bash script and wait for each loop to finish

我正在尝试编写一个脚本,我们称之为 pippo.R。 pippo.R 目标是 运行 另一个脚本 (for.sh) 在 for 循环中使用两个值进行并行化: nPerm= 脚本必须执行的总次数 运行 permAtTime=同时可以运行的脚本数量。 一件非常重要的事情是等待每个循环结束,这就是为什么我添加了一个存储所有 PID 的文件,然后我使用等待函数等待它们中的每一个。此脚本的主要问题是以下错误:

./wait.sh: line 2: wait: pid 836844 is not a child of this shell

为了重现性,您可以将以下文件放入文件夹中: pippo.R

nPerm=10
permAtTime=2
cycles=nPerm/permAtTime
for(i in 1:cycles){
  d=1
  system(paste("./for.sh ", i," ",permAtTime,sep=""))
}

for.sh

#!/bin/bash
for X in $(seq )
do
 nohup ./script.sh $(($X +( -1)* )) &
 echo $! >> ./save_pid.txt
done
./wait.sh

wait.sh

#!/bin/bash
while read p; do wait $p; done < ./save_pid.txt

运行 Rscript pippo.R 你会得到解释的错误。我知道有一个并行函数可以帮助我解决这个问题,但由于多种原因我无法使用该程序包。 谢谢

您不需要跟踪 PID,因为如果您不带任何参数调用 wait,脚本将等待所有子进程完成。

#!/bin/bash
for X in $(seq )
do
 nohup ./script.sh $(($X +( -1)* )) &
done
wait