Madproject

Always know where your towel is.

Magento Cron Debugging with XDebug and PHPStorm

I recently needed to debug some Magento code from both the command line and stuff executed by cron. Inserting Mage::log() here and there wasn’t exactly efficient or satisfying.

I’ve always used XDebug with PHPStorm (which I use solely for debugging) but all code was always executed within the browser.

Long story short, if you want to debug code in the CLI or executed by cron there are a few extra steps you need to take.

Note that many of these settings are system/config dependent but there’s still a pretty good chance this will work for you. For reference, my dev setup is a CentOS x64 6.4 LAMP config running in Virtual Box and managed by Vagrant.

php.ini

zend_extension="/usr/lib64/php/modules/xdebug.so"
xdebug.remote_enable=1
xdebug.idekey = "PHPSTORM"
xdebug.remote_host=x.x.x.x
  • Replace x.x.x.x with your physical machine IP
  • Replace PHPSTORM with your IDE key (PHPSTORM by default)

CLI

$ PHP_IDE_CONFIG="serverName=mysite.dev" XDEBUG_CONFIG="remote_host=x.x.x.x idekey=PHPSTORM" php script.php
  • Replace mysite.dev with the PHP->Servers->Name in PHPStorm
  • Replace x.x.x.x with your physical machine IP

CLI Alias
Save yourself some keystrokes by aliasing the command. Add the following to your global bashrc which is typically in /etc/bashrc:

alias phpdebug='PHP_IDE_CONFIG="serverName=mysite.dev" XDEBUG_CONFIG="remote_host=x.x.x.x idekey=PHPSTORM" /usr/bin/php

You can now debug in the CLI using:

phpdebug script.php

crontab

SHELL="/bin/bash"
PHP_IDE_CONFIG="serverName=mysite.dev"
XDEBUG_CONFIG="remote_host=x.x.x.x idekey=PHPSTORM"
* * * * * /usr/bin/php -f /vagrant/cron.php
  • Replace mysite.dev with the PHP->Servers->Name in PHPStorm
  • Replace x.x.x.x with your physical machine IP
  • Replace PHPSTORM with your IDE key (PHPSTORM by default)

Couple of notes

  • You’ll want to make sure PHPStorm is configured with Xdebug
  • Make sure PHPStorm is listening for XDebug by clicking the phone icon in the PHPStorm toolbar
  • Comment out the PHP_IDE_CONFIG and XDEBUG_CONFIG in crontab when not debugging
  • PHPStorm may debug every request even if there’s no break point. To fix this: Preferences->PHP->Debug and uncheck Force break at the first line when no path mapping specified

Leave a Reply

Your email address will not be published. Required fields are marked *