Proper shell execution in PHP

前端 未结 4 673
情歌与酒
情歌与酒 2021-02-14 16:53

The problem

I was using a function that made use of proc_open() to invoke shell commands. It seems the way I was doing STDIO was wrong and sometimes cause

4条回答
  •  孤街浪徒
    2021-02-14 17:47

    The whole problem with hanging in stream_get_contents is in the way how process is created. The correct way is to open STDOUT with read/write mode of pipe, eg:

    $descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "rw"), 2 => array ("pipe", "rw"));
    //Open the resource to execute $command
    $t->pref = proc_open($command,$descriptor,$t->pipes);
    //Set STDOUT and STDERR to non-blocking 
    stream_set_blocking ($t->pipes[0], 0);
    stream_set_blocking ($t->pipes[1], 0);
    

    This is obvious that when stream_get_contents wants to read the STDOUT pipe it needs read mode. The same bug with hang/freeze/block is in this nice class https://gist.github.com/Arbow/982320

    Then blocking disappears. But read does not read nothing.

提交回复
热议问题