2

我目前正在从我的脚本中生成大约 100 个文件,我想以 20 个批次迭代这些文件,并通过另一个脚本执行它们,然后在我完成后删除这些文件(清理)我相信 GNU Parallel 可以做到这一点但我不确定该怎么做?

# test if files exists and run
if [ "$(ls -A ${base_dir}/schedule)" ]; then

    while [ "$(ls -A ${base_dir}/schedule)" ]; do

        # current run of 20 files
        batch=`ls ${base_dir}/schedule | head -n 20`

        # parallel run on 4 processors
        parallel -j4 ./script.sh ${batch} ::: {1..20}

        # cleanup
        for file in "${batch}"; do
            rm "${base_dir}/schedule/${file}"
        done

    done
fi

预期输出将类似于

# running first batch of twenty
 ./scipt.sh 1466-10389-data.nfo # after file has finished, rm 1466-10389-data.nfo
 ./scipt.sh 1466-10709-data.nfo # etc
 ./scipt.sh 1466-11230-data.nfo # etc
 ./scipt.sh 1466-11739-data.nfo
 ./scipt.sh 1466-11752-data.nfo
 ./scipt.sh 1466-13074-data.nfo
 ./scipt.sh 1466-14009-data.nfo
 ./scipt.sh 1466-1402-data.nfo
 ./scipt.sh 1466-14401-data.nfo
 ./scipt.sh 1466-14535-data.nfo
 ./scipt.sh 1466-1588-data.nfo
 ./scipt.sh 1466-17012-data.nfo
 ./scipt.sh 1466-17611-data.nfo
 ./scipt.sh 1466-18688-data.nfo
 ./scipt.sh 1466-19469-data.nfo
 ./scipt.sh 1466-19503-data.nfo
 ./scipt.sh 1466-21044-data.nfo
 ./scipt.sh 1466-21819-data.nfo
 ./scipt.sh 1466-22325-data.nfo
 ./scipt.sh 1466-23437-data.nfo

# wait till all are finished, OR queue up next file so  all times
# twenty files are running at until the directory is empty
4

2 回答 2

1

如果我正确理解您想要做什么,并且如果schedule没有连续创建文件,则可以用这两行替换脚本(未经测试)

ls -A ${base_dir}/schedule | xargs -n 1 -P 4 ./script.sh 
rm "${base_dir}/schedule/*"
于 2013-10-24T17:16:49.117 回答
1

我的猜测是您希望 20 个脚本不断并行运行:

ls -A ${base_dir}/schedule | parallel -j20 ./script.sh {/}\; rm {}

您的 while 循环让我有点困惑:是否需要它,因为在您运行时可能会添加更多文件?如果是这样,您需要添加该 while 循环:

while [ "$(ls -A ${base_dir}/schedule)" ]; do
  ls -A ${base_dir}/schedule | parallel -j20 ./script.sh {/}\; rm {}
done

浏览教程http://www.gnu.org/software/parallel/parallel_tutorial.html您的命令行会因此而爱上您。

于 2013-10-25T13:16:56.667 回答