1

我目前有一个 perl 脚本,我试图用它来启动三个(或更多)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 脚本。我没有收到任何错误消息。

非常感激任何的帮助。

4

2 回答 2

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);
}
于 2012-07-23T09:02:51.237 回答
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.

于 2012-07-23T09:00:47.980 回答