【问题标题】:how to launch multiple fire and forget PHP scripts with Perl?如何使用 Perl 启动多个火灾并忘记 PHP 脚本?
【发布时间】:2012-07-23 08:51:52
【问题描述】:

我目前有一个 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 脚本。我没有收到任何错误消息。

任何帮助将不胜感激。

【问题讨论】:

标签: perl fire-and-forget


【解决方案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);
}

【讨论】:

  • 代替fork 然后exec 一个简单的system 会更好。
  • -1 : 这个“一劳永逸”是怎么做到的? waitpid 将阻塞直到子进程完成。
【解决方案2】:

来自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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2021-05-01
    • 2014-04-09
    • 2013-08-22
    • 2022-01-04
    • 1970-01-01
    • 2023-04-04
    • 2014-09-02
    相关资源
    最近更新 更多