RE: exec() and redirect output of program [SOLVED]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tijnema ! wrote:
> On 5/1/07, Brad Fuller <bfuller@xxxxxxxxxxxxxxxx> wrote:
>> Brad Fuller wrote:
>>> Daniel Brown wrote:
>>>>     This way just lets it do it's own thing, with no output, and
>>>> PHP won't hang.  It'll continue from the CLI after the HTTP
>>>> session is over. 
>>>> 
>>>> <?
>>>> exec('php test.php > /dev/null 2>&1 &'); ?>
>>>> 
>>>> 
>>>> On 5/1/07, Brad Fuller <bfuller@xxxxxxxxxxxxxxxx> wrote:
>>>>> 
>>>>> 
>>>>> I found this on PHP.net:
>>>>> 
>>>>> http://us.php.net/manual/en/function.exec.php
>>>>> 
>>>>> Note: If you start a program using this function and want to leave
>>>>> it running in the background, you have to make sure that the
>>>>> output of that program is redirected to a file or some other
>>>>> output stream or else PHP will hang until the execution of the
>>>>> program ends. 
>>>>> 
>>>>> 
>>>>> This is what I want... I want to execute another PHP script from
>>>>> the CLI, pass it a parameter and let it go to town after the HTTP
>>>>> request closes. 
>>>>> 
>>>>> Can someone please illustrate how I can make this work?
>>>>> 
>>>>> Thx,
>>>>> 
>>>>> Brad
>>>>> 
>>>>> --
>>>>> PHP General Mailing List (http://www.php.net/) To unsubscribe,
>>>>> visit: http://www.php.net/unsub.php
>>> 
>>> 
>>> It seems the script is calling itself even though I'm specifying a
>>> different script to run... 
>>> 
>>> test2.php
>>> 
>>> <?php echo "Hello, World!"; ?>
>>> 
>>> 
>>> test1.php
>>> 
>>> <?php
>>>       if( !isset($_POST['account_id']) || $_POST['account_id'] ==
>>>               "" ) { echo "account_id is required.";
>>>               exit;
>>>       }
>>> 
>>>       // more stuff here...
>>> 
>>>       exec("/usr/bin/php -q /path/to/test2.php", $output); //
>>> should run test2.php 
>>> 
>>>       echo "<pre>";
>>>       print_r($output);
>>>       echo "</pre>";
>>> 
>>>> 
>>> 
>>> 
>>> http://www.example.com/test1.php
>>> 
>>> Expected Result:
>>> 
>>> Array
>>> (
>>>     [0] => Hello, World!
>>> )
>>> 
>>> 
>>> Actual Result:
>>> 
>>> Array
>>> (
>>>     [0] => X-Powered-By: PHP/5.2.1
>>>     [1] => Content-type: text/html
>>>     [2] =>
>>>     [3] => account_id is required.
>>> )
>>> 
>>> Can anyone explain this and possibly help me find a solution?
>>> 
>>> Thx,
>>> 
>>> Brad
>> 
>> P.S. I am posting a form to the test1.php page with a valid
>> account_id etc.; after re-reading the message I thought someone
>> might think it's printing that result because nothing is posted.
>> 
>> Update:
>> 
>> I also found a file called "error_log" in the folder where test2.php
>> resides, full of several of these lines:
>> 
>> [01-May-2007 14:12:52] PHP Warning:  Zend Optimizer does not support
>> this version of PHP - please upgrade to the latest version of Zend
>> Optimizer in Unknown on line 0
>> 
>> Could that have something to do with why the script is calling on
>> itself instead of running the specified php script?
>> 
>> I recently had the hosting company rebuild PHP, first they did
>> --enable-suexec (to run PHP as CGI) and then later rebuilt again to
>> --enable-pcntl and --enable-sigchild, as I thought I would be needing
>> that functionality.  Did that break the CLI?
>> 
>> Please help, Thx.
>> 
>> Brad
> 
> 
> It seems that the php binary isn't the same version as the
> php library used in the webserver and so that there's a
> problem loading Zend. Are you sure that the PHP binary is
> also replaced when they reinstalled PHP?
> 
> Tijnema


Well, I finally got it working... I simply call "php" instead of using the
full path "/usr/bin/php".  When I type "which php" from the shell I get
"/usr/local/bin/php" so I'm not sure if the CLI binary maybe got moved when
we switched to CGI mode or what, anyway I still get the Zend Optimizer
Warning message when I run the script from the command line but for some
reason it doesn't write to error_log when the script is called from the
exec() function.. Maybe that's simply because the errors are being
redirected to /dev/null... But anyway it works now :)  I will call up our
hosting company and see if we can do something about that Zend Optimizer
warning.

Thanks Daniel and Tijnema for the help.

Cheers,

Brad

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux