Asynchronous shell exec in PHP

前端 未结 13 2037

I\'ve got a PHP script that needs to invoke a shell script but doesn\'t care at all about the output. The shell script makes a number of SOAP calls and is slow to complete,

13条回答
  •  太阳男子
    2020-11-22 01:30

    To all Windows users: I found a good way to run an asynchronous PHP script (actually it works with almost everything).

    It's based on popen() and pclose() commands. And works well both on Windows and Unix.

    function execInBackground($cmd) {
        if (substr(php_uname(), 0, 7) == "Windows"){
            pclose(popen("start /B ". $cmd, "r")); 
        }
        else {
            exec($cmd . " > /dev/null &");  
        }
    } 
    

    Original code from: http://php.net/manual/en/function.exec.php#86329

提交回复
热议问题