2012-07-23 47 views
1

我现在有这我试图用推出每三个(或更多)的PHP脚本从数据库提供的一组参数,Perl脚本:如何用Perl启动多个fire并忘记PHP脚本?

$sql = "SELECT id,url,added,lasttotal,lastsnapshot,speed,nextsnapshot FROM urls WHERE DATE(NOW()) > DATE(nextsnapshot) LIMIT 0,3"; 
$sth = $dbh->prepare($sql); 
$sth->execute or print "SQL Error: $DBI::errstr\n"; 

my ($urlID, $url, $added,$lastTotal,$lastSnapshot,$lastSpeed,$nextsnapshot); 

$sth->bind_col(1, \$urlID); 
$sth->bind_col(2, \$url); 
$sth->bind_col(3, \$added); 
$sth->bind_col(4, \$lastTotal); 
$sth->bind_col(5, \$lastSnapshot); 
$sth->bind_col(6, \$lastSpeed); 
$sth->bind_col(7, \$nextsnapshot); 

while ($sth->fetch) { 
    $myexec = "php /usr/www/users/blah/blah/launch_snapshot.php '$url' $urlID '$added' $lastTotal '$lastSnapshot' $lastSpeed".' /dev/null 2>&1 &'; 

    exec ($myexec)  or print "\n Couldn't exec $myexec: $!"; 
} 

我不关心任何结果PHP脚本,我只需要一次启动它们,或者延迟很小。

获取正常工作并返回三组唯一值。但是,它似乎从来没有推出第一个PHP脚本。我没有收到任何错误消息。

任何帮助将不胜感激。

+2

从[官方的Perl FAQ](http://faq.perl.org):如何启动一个进程背景?](http://learn.perl.org/faq/perlfaq8.html#How-do-I-start-a-process-in-the-background-)||| [我如何在Perl中触发并忘记进程?](http://stackoverflow.com/questions/2133910/how-can-i-fire-and-forget-a-process-in-perl) – daxim 2012-07-23 11:54:23

回答

1

你可以使用fork或只是system

使用fork

foreach($sth->fetch) { 
    my $pid = fork(); 
    if($pid) { # Parent 
    waitpid($pid, 0); 
    } elsif ($pid == 0) { # A child 
    $myexec = "..."; 
    exec($myexec) or print "\n Couldn't exec $myexec: $!"; 
    exit(0); # Important! 
    } else { 
    die "couldn't fork: $!\n"; 
    } 
} 

使用system

foreach($sth->fetch) { 
    $myexec = "..."; 
    system($myexec); 
} 
+0

而不是' fork'然后'exec'是一个简单的'system',IMO会更好。 – dgw 2012-07-23 09:07:21

+0

完美答案,谢谢! – AnthonyVader 2012-07-23 09:16:32

+2

-1:这个“即忘即忘”是怎样的? 'waitpid'将会阻塞,直到子进程完成。 – Zaid 2012-07-23 09:39:49

0

perldoc -f exec

exec LIST 
    exec PROGRAM LIST 
      The "exec" function executes a system command and never 
      returns-- use "system" instead of "exec" if you want it to 
      return. It fails and returns false only if the command does 
      not exist and it is executed directly instead of via your 
      system's command shell (see below). 

你想system(或fork)不exec.