Jump to content

How to execute two process using proc open in PHP


skyprog

Recommended Posts

I am executing two child process using proc open in PHP. After both the process are created I access the stdout of first process  but it gives error that the descriptor is not valid, where as the output of second process is displayed. It seems that the pipes that were attached with stdout of first process are no more connected with it and instead they are pointing to second process. How can I have correct display of both the processes?
      
PHP Code


   

<?php                  
      echo "Executing a process\n";
      $cwd = '/var/www/html/test-9-5-14';
      $exe_command = ('/var/www/html/test-9-5-14/./hello');
      $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin -> for execution
        1 => array("pipe", "w"),  // stdout -> for execution
        2 => array("pipe", "w") // stderr
      );
    
      $process = proc_open($exe_command, $descriptorspec, $pipes, $cwd);//creating child process
      stream_set_blocking($pipes[1], 0);
      stream_set_blocking($pipes[0], 0);
      stream_set_blocking($pipes[2], 0);
      sleep(1);
    
      if (is_resource($process)) {
        echo "Process Created";
        $stdout1 = array($pipes[1]);//save stdout in a variable defined above
        print_r ($stdout1);
      }
      $exe_command = ('/var/www/html/test-9-5-14/./add');
      $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin -> for execution
        1 => array("pipe", "w"),  // stdout -> for execution
        2 => array("pipe", "w") // stderr
      );
      $process = proc_open($exe_command, $descriptorspec, $pipes1, $cwd);//creating child process
      stream_set_blocking($pipes1[1], 0);
      stream_set_blocking($pipes1[0], 0);
      stream_set_blocking($pipes1[2], 0);
      sleep(1);
    
      if (is_resource($process)) {
        echo "Process 2 Created";
        $stdout2 = array($pipes1[1]);//save stdout in a variable defined above
        print_r ($stdout2);
      }
      $s = fgets($stdout1[0]);
      echo $s;
    
      $s = fgets($stdout2[0]);
      echo $s;
    
    ?>

Output


   

Executing a process
    Process CreatedArray
    (
      [0] => Resource id #5
    )
    Process 2 CreatedArray
    (
      [0] => Resource id #9
    )
    PHP Warning:  fgets(): 5 is not a valid stream resource in /var/www/html/test-9-5-14/test.php on line 70
    --Enter two integers >

--Enter two integers > is the output of my second process whereas the first process stdout descriptor 5 is invalid.

Any ideas please. Thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.