2

我有一个要下载它们的文件 URL 列表:

http://somedomain.com/foo1.gz
http://somedomain.com/foo2.gz
http://somedomain.com/foo3.gz

我想要对每个文件执行以下操作:

  1. wget与和并行下载 foo1,2 nohup..
  2. 每次完成下载过程时myscript.sh

我所拥有的是:

#! /usr/bin/perl

@files = glob("foo*.gz");

foreach $file (@files) {
   my $downurls = "http://somedomain.com/".$file;
   system("nohup wget $file &");
   system("./myscript.sh $file >> output.txt");
}

问题是我无法告诉上述管道文件何时完成下载。所以现在它 myscript.sh 没有得到正确执行。

实现这一目标的正确方法是什么?

4

3 回答 3

2

为什么要使用 perl。改用 bash。下面只是一个示例。

#!/bin/bash

for file in foo1 foo2 foo3
do
    wget http://samedomain.com/$file.gz .

    if [ -f $file.gz ];
    then
        ./myscript.sh $file.gz >> output.txt
    fi
done
于 2010-04-16T10:40:45.017 回答
1

尝试使用 组合命令&&,以便第二个仅在第一个成功完成后运行。

system("(nohup wget $file  && ./myscript.sh $file >> output.txt) &");
于 2010-04-16T06:04:07.967 回答
1

如果您想要并行处理,您可以通过分叉自己完成,或使用内置模块为您处理。试试Parallel::ForkManager。您可以在How can I manage a fork pool in Perl?,但该模块的 CPAN 页面将包含真正有用的信息。你可能想要这样的东西:

use Parallel::ForkManager;

my $MAX_PROCESSES = 8; # 8 parallel processes max
my $pm = new Parallel::ForkManager($MAX_PROCESSES);

my @files = glob("foo*.gz");

foreach $file (@all_data) {
  # Forks and returns the pid for the child:
  my $pid = $pm->start and next; 

  my $downurls = "http://somedomain.com/".$file;
  system("wget $file");
  system("./myscript.sh $file >> output.txt");

  $pm->finish; # Terminates the child process
}

print "All done!\n";
于 2010-04-16T16:46:24.173 回答