如何通过主脚本控制其他脚本,并在它们运行时传递给它们参数

I have a main.php and test.php.

  1. test.php should be executed by main.php
  2. both scripts must be run infinite.
  3. main.php must checks in periods of time that test.php is running or not and if it isnt running (in case of occuring errors) execute it again.
  4. I must have error logs too.
  5. if main.php recieve 'test stop' it sends 'close' to test.php and test.php must stop (I dont know how send my orders (such as 'test stop') to main.php after that executed?)

I have this samples:

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();

}
?>

ok this was summary of my codes but they dont work right.

for example when script arrive to this line $status = trim(fgets(STDIN)); in test.php it waits until an input and if we dont send any input for it, script stops and dont run rest of code but I want script runs in the loop and executes orders until main.php pass an input to him.

I'm working on windows.

I'd say that PHP isn't the best tool for what you're trying to accomplish. Why don't you write a program in C or Visual Basic or something?

But it's solvable in PHP too: I'd suggest to create your own error-handling function and assign it in test.php via the set_error_handler('my_custom_error_function') function.

In my_custom_error_function() you can log the error and restart test.php Appending a line to a logfile can be done via file_put_contents('.\error.log', $error_string, FILE_APPEND)

fgets() expects an open file handle. So you may want to check your routines (or provide more code). You may want to look into file_get_contents() too.