使用Bash连续运行PHP脚本

使用Bash连续运行PHP脚本,php,bash,scripting,memory-leaks,Php,Bash,Scripting,Memory Leaks,我有一个长期运行的PHP脚本,它有一个内存泄漏,导致它部分失败。该脚本使用了第三方库,我一直无法找到泄漏源 我想做的是创建一个bash脚本,该脚本持续运行PHP脚本,一次处理1000条记录,直到脚本返回一个退出代码,表示它已经完成了对所有记录的处理。我认为这应该有助于我避免内存泄漏,因为脚本将运行1000条记录,然后退出,然后为另外1000条记录启动一个新进程 我对Bash不太熟悉。这可能吗?如何从PHP脚本获得输出 在伪代码中,我想到了以下几点: do: code = exec('.../

我有一个长期运行的PHP脚本,它有一个内存泄漏,导致它部分失败。该脚本使用了第三方库,我一直无法找到泄漏源

我想做的是创建一个bash脚本,该脚本持续运行PHP脚本,一次处理1000条记录,直到脚本返回一个退出代码,表示它已经完成了对所有记录的处理。我认为这应该有助于我避免内存泄漏,因为脚本将运行1000条记录,然后退出,然后为另外1000条记录启动一个新进程

我对Bash不太熟悉。这可能吗?如何从PHP脚本获得输出

在伪代码中,我想到了以下几点:

do:
  code = exec('.../script.php')
   # PHP script would print 0 if all records are processed or 1 if there is more to do
while (code != 0)

美元?提供bash中程序的退出代码

你可以做类似的事情

while /bin/true; do
  php script.php
  if [ $? != 0 ]; then
     echo "Error!";
     exit 1;
  fi
done
你甚至可以:

while php script.php; do
   echo "script returned success"
done

美元?提供bash中程序的退出代码

你可以做类似的事情

while /bin/true; do
  php script.php
  if [ $? != 0 ]; then
     echo "Error!";
     exit 1;
  fi
done
你甚至可以:

while php script.php; do
   echo "script returned success"
done

使用一个简单的
until
循环自动测试PHP脚本的退出状态

#!/bin/sh
until script.php
do
   :
done
冒号仅仅是一个空运算符,因为您实际上不想在循环中执行任何其他操作。 执行命令
script.php
直到返回零(也称为true)。如果脚本返回0以指示未完成,而不是1,则可以使用
while
而不是
直到

PHP脚本的输出将转到standard out和standard error,因此您可以使用一些I/O重定向来包装shell脚本的调用,以将输出保存在一个文件中。例如,如果脚本名为
loop.sh
,则只需运行:

./loop.sh > output.txt
当然,您可以直接在PHP脚本中控制输出文件;您只需记住打开文件进行追加


您可能想问一个关于如何调试PHP内存泄漏的单独问题:-)

使用一个简单的
until
循环来自动测试PHP脚本的退出状态

#!/bin/sh
until script.php
do
   :
done
冒号仅仅是一个空运算符,因为您实际上不想在循环中执行任何其他操作。 执行命令
script.php
直到返回零(也称为true)。如果脚本返回0以指示未完成,而不是1,则可以使用
while
而不是
直到

PHP脚本的输出将转到standard out和standard error,因此您可以使用一些I/O重定向来包装shell脚本的调用,以将输出保存在一个文件中。例如,如果脚本名为
loop.sh
,则只需运行:

./loop.sh > output.txt
当然,您可以直接在PHP脚本中控制输出文件;您只需记住打开文件进行追加


您可能想问一个关于如何调试PHP内存泄漏的单独问题:-)

您必须使用bash吗?您可能可以使用PHP实现这一点:

while (true) {
  $output = exec('php otherscript.php', $out, $ret);
}

$ret变量将包含脚本的退出代码。

是否必须使用bash?您可能可以使用PHP实现这一点:

while (true) {
  $output = exec('php otherscript.php', $out, $ret);
}
$ret变量将包含脚本的退出代码。

您可以编写:

#!/bin/bash 

/usr/bin/php prg.php # run the script.
while [  $? != 0 ]; do # if ret val is non-zero => err occurred. So rerun.
  /usr/bin/php prg.php
done
你可以写:

#!/bin/bash 

/usr/bin/php prg.php # run the script.
while [  $? != 0 ]; do # if ret val is non-zero => err occurred. So rerun.
  /usr/bin/php prg.php
done

用PHP实现的解决方案改为:

do {
    $code = 1;
    $output = array();
    $file = realpath(dirname(__FILE__)) . "/script.php";
    exec("/usr/bin/php {$file}", $output, $code);

    $error = false;
    foreach ($output as $line) {
        if (stripos($line, 'error') !== false) {
            $error = true;
        }
        echo $line . "\n";
    }
} while ($code != 0 && !$error);

用PHP实现的解决方案改为:

do {
    $code = 1;
    $output = array();
    $file = realpath(dirname(__FILE__)) . "/script.php";
    exec("/usr/bin/php {$file}", $output, $code);

    $error = false;
    foreach ($output as $line) {
        if (stripos($line, 'error') !== false) {
            $error = true;
        }
        echo $line . "\n";
    }
} while ($code != 0 && !$error);

第二个示例很好,不需要使用$?您不必使用
/bin/true
,Bash有一个
true
内置。您也不需要使用
$?
。第二个示例是正确的方法。在shell中,程序在成功时必须返回0。记住循环:)第二个例子可以很好地工作,不需要使用$?您不必使用
/bin/true
,Bash有一个
true
内置。您也不需要使用
$?
。第二个示例是正确的方法。在shell中,程序在成功时必须返回0。记住这个循环:)好主意。我决定走这条路。看看我的解决方案。好主意。我决定走这条路。看看我的解决方案。