Quantcast
Channel: Anil Konsal
Viewing all articles
Browse latest Browse all 10

Executing PHP script from shell using exec() in a PHP script

$
0
0

If you are trying to execute a php script from shell using exec() or passthru() functions within a php script then you may end up with a hung apache thread or non-execution of the to be executed script.

I was doing the same in one of my projects where I need to import a big list of items (half million) in a project built on Yii Framework using a CSV upload. Coding was written, but the import failed due to large number of validations to be run before importing. So, I thought to create a Console App in Yii and then call this Console App from my Yii Controller action like this:

1
exec('php '.Yii::app()->basePath.'/yiic.php '. $consoleApp .' > /dev/null 2>&1 &');

This worked well on my MacBook Pro but when the same code was executed on the live server (CentOs with PHP 5.2.17), the script did not execute at all. I spent full 4 days and tried all sorts of techniques to get it executed but in vain.

Then, I replaced php with php-cli in the command

1
exec('php-cli '.Yii::app()->basePath.'/yiic.php '. $consoleApp .' > /dev/null 2>&1 &');

And it worked well! Its a bug in PHP that you cannot use php interpreter to execute another php script when it is already executing your script from http or browser. So, the workaround is use ‘php-cli

Happy Yii-ing !


Viewing all articles
Browse latest Browse all 10

Trending Articles