Search code examples
phpwindowsbackground-processpopen

How to debug popen() not working?


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.


Solution

  • 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);