Can't execute PHP script using PHP exec

后端 未结 6 1415
臣服心动
臣服心动 2020-11-30 03:42

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 04:18

    The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.

    I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:

    $script = $argv[1]
    $params = implode(' ', array_slice($argv, 2));
    $cmd    = "{$script} {$params} > /dev/null &";
    
    $output = array();
    $return = 0;
    exec("php {$cmd}", $output, $return);
    
    exit((int)$return);
    

    And then you call it using:

    exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
    

    It´s the only way I managed to get this working.

提交回复
热议问题