【问题标题】:PHP & Cron - chdir not workingPHP & Cron - chdir 不工作
【发布时间】:2012-04-08 15:18:13
【问题描述】:

我有一个作为守护进程运行的 PHP 脚本。 8 个线程同时运行。

为确保始终运行 8 个线程,以下 PHP 脚本从 cron 运行,该脚本与 daemon.php 脚本位于同一目录中:

<?php

chdir('/root/fb');
if (file_exists('pause')) die();

exec('ps ax | grep -v grep | grep daemon.php',$output);
$output=implode("\n",$output);
$num=8-substr_count($output,'daemon.php');
if ($num>0)
 {
 for($run=0; $run<$num; $run++)
  {
  exec('php daemon.php > /dev/null 2>&1 &');
  sleep(20);
  }
 }

?>

上面将愉快地运行 daemon.php 脚本,但随后发生了一些奇怪的事情,并且 daemon.php 脚本本身有时会认为它在不同的目录中,而不是其他时候。具体来说,我使用了很多exec 函数来执行其他应用程序,其中许多(但不是全部)认为它们回到了原始目录而不是/root/fb

如果我直接使用目录中的php daemon.php 执行 daemon.php,则不会发生这种情况,一切都按预期工作。

非常不正常的行为。如何设置它,以使 cron 作业的效果与直接从其目录中执行脚本时的效果完全相同?

【问题讨论】:

    标签: php cron chdir


    【解决方案1】:

    也许简化和更具防御性的编程方法可以帮助您缩小问题范围。

    <?php
    
    if (!chdir('/root/fb')) die("couldn't chdir");
    file_exists('pause')) die("paused");
    
    exec('pgrep -f "php daemon.php"',$output);
    $output = implode("\n",$output);
    $num = 8 - count($output);
    for($run = 0; $run < $num; $run++) {
       /*exec('php daemon.php > /dev/null 2>&1 &');*/
       passthru('echo $PWD');
       sleep(20);
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多