【发布时间】:2013-07-26 02:48:46
【问题描述】:
我有一个 main.php 和 test.php。
- test.php 应该由 main.php 执行
- 这两个脚本都必须无限运行。
- main.php 必须在一段时间内检查 test.php 是否正在运行,如果它没有运行(以防出现错误)再次执行。
- 我也必须有错误日志。
- 如果 main.php 收到
'test stop'它会将'close'发送到 test.php 并且 test.php 必须停止(我不知道执行后如何将我的订单(例如'test stop')发送到main.php?)
我有这个样本:
main.php:
<?php
function execute(){
$desc = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('file', 'log.txt', 'a')
);
$cmd = "start /b C:\wamp\bin\php\php5.4.3\php.exe test.php";
$p = proc_open($cmd, $desc, $pipes);
$res[0] = $p;
$res[1] = $pipes;
return $res;
}
$res = execute();
while(1) {
$status = proc_get_status($res[0]);
if (!$status['running']) {
$res = execute();
}
if ( trim(fgets(STDIN)) == 'stop test' ) {
fwrite($res[1][0], 'close');
fclose($res[1][0]);
fclose($res[1][1]);
fclose($res[1][2]);
proc_close($res[0]);
break;
}
}
?>
test.php:
<?php
while (1) {
// ---------
// other commands
// ---------
// ---------
$status = trim(fgets(STDIN));
if ($status == 'close') exit();
}
?>
好的,这是我的代码摘要,但它们不能正常工作。
例如,当脚本到达 test.php 中的这一行 $status = trim(fgets(STDIN)); 时,它会等到输入,如果我们不发送任何输入,脚本会停止并且不运行其余代码,但我希望脚本在循环中运行并执行命令,直到 main.php 将输入传递给他。
我在 Windows 上工作。
【问题讨论】:
-
为什么连一个简单的答案都没有人回答我??!! :((
-
如果您的解决方案是针对 linux 的,那没问题。在这里说。我可以将它转换为 Windows 语法。请帮助我:))
-
明天我开始
bounty。
标签: php command-line stdin proc-open