douye1940 2015-02-25 18:13
浏览 399
已采纳

如何调试popen()无法正常工作?

On my Linux server I use shell_exec() to run scripts asynchronously. This function is not available on Windows, which I use for development, and so I am trying to use popen() instead.

I am running Windows 10 x64, WampServer 2.5 x32 (PHP 5.5.12).

I have the following code in a PHP file:

$cmd = '"C:\wamp\bin\php\php5.5.12\php.exe" "C:\wamp\www\path\to\file.php" "arg1" "arg2"';

        echo 'Running on DEV MACHINE: ' . $cmd . PHP_EOL;
        $handle = popen("start /B " . $cmd, "r");
        if ($handle === FALSE) {
            die("Unable to execute $cmd");
        }
        pclose($handle);

The script executes without any errors, but the PHP files are never run. I know they aren't being run because I put an SQL query at the very top to update a field in a database to indicate they have been run.

As you can see in the above code, I print out the command being passed to popen(). If I paste that command into command prompt and execute it, the scripts run fine. This proves the issue lies with popen().

Am I using popen() incorrectly somehow? How can I debug what the problem is? Normally I would debug using XDebug, but there's no way to use that in this situation.

  • 写回答

1条回答 默认 最新

  • dongmu4591 2015-03-02 22:26
    关注

    I was never able to get my code working, but I found an alternative method from another answer on here and based on that changed my code to use the following and it works:

    $WshShell = new \COM("WScript.Shell");
    $oExec = $WshShell->Run($cmd, 0, false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline