Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Debugger hangs when calling exec('php ...')

I have a PHP script in which, at one point, I call exec() on another PHP script. This runs perfectly fine, but it hangs when using the XDebug debugger in NetBeans. This is causing me all sorts of problems, as I'm unable to debug the entire application.

Here is a trivial example:

test1.php

<?php

$output = array();
$status = 0;
exec('echo "Running inside test 1"', $output, $status);
exec('php ' . __DIR__ . '/test2.php', $output, $status); // Debugger hangs here

var_dump($output);
var_dump($status);
?>

test2.php

<?php 
echo "Running inside test 2" . PHP_EOL;
?>

If I run test1.php, it runs to completion and produces the expected output.

If I debug test1.php, it hangs on the exec('php ...') line.

I've tried this with shell_exec, and get the same problem. I've also tried exec'ing on a .sh file or other executable, with no issues.

At first I thought that xdebug was somehow attaching to the new PHP process started by the exec and locking it, but I've checked my php.ini and have xdebug.remote_autostart=off.

I'm aware that calling a PHP script via exec() is a strange way of doing things; it is actually an externally provided PHAR file which we are exec'ing in the real codebase, but the trivial example above has the same symptom, so I'm assuming it is the same problem.

In case it is relevant, I'm using PHP 5.5.13, Xdebug 2.2.3, Netbeans 7.3.1, Ubuntu 12.0.4.

like image 693
geekydel Avatar asked Dec 17 '14 11:12

geekydel


People also ask

Why is Xdebug not working?

Xdebug cannot connect to PhpStorm This means that Xdebug tries to connect to the host and can't make the connection. To fix the issue, set xdebug. remote_connect_back=0 ( xdebug. discover_client_host=false for Xdebug 3) and make sure that xdebug.

How do I know if Xdebug is running?

Verify that Xdebug is properly running by checking again with phpinfo() or php -v as explained above. Note that 9003 is the default port. If this port is used by another service on your system, change it to an unused port. After adding these settings, restart your webserver again.


1 Answers

This happens because when you exec second script, xdebug is already busy, so innder script stalls, and execution of outer script cannot be continued.

To deal with this problem:

  1. comment off the xdebug settings in php ini and check environment variable XDEBUG_CONFIG not to contain any
  2. start main script with xdebug parameters (php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1)
  3. exec second script without xdebug parameters: exec('php script.php')

To debug inner script, start first script without xdebug, and exec with xdebug, vice versa.

like image 109
Mikhail Zhuravlev Avatar answered Sep 28 '22 10:09

Mikhail Zhuravlev