From: Richard Quadling on
On 15 July 2010 16:51, Leonardo <leobasilio(a)oi.com.br> wrote:
> Hi everybody. I need to use exec() to run a background php script, but it's
> not working properly. Take a look at this sample:
>
> a.php
>   <?
>
>   echo ' File A (1) ';
>
>   exec('php b.php > output.txt &');
>
>   echo ' File A (2) ';
>
>   ?>
>
> b.php
>   <?
>
>   echo 'File B';
>
>   ?>
>
> output.txt (begins with 64 null bytes before the following)
>    File A (2)
>
> I would expect the output's content to be "File B", but it's not happening.
> I came across this problem because I have a mailing application which
> stopped working after being moved to a new host. The messages were sent by a
> background script launched on user's demand. Like this:
>
> SendMessages.php
>   <?
>      ...
>      exec('php Daemon.php -x ' . $USERID . ' > /dev/null &');
>      ...
>   ?>
>
> Now it won't work, and I got no clue about what's going on. What now?
>
> Thank you.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

I'm on Windows XP SP3 and using

PHP 5.3.3RC3 (cli) (built: Jul 15 2010 02:00:11)
Copyright (c) 1997-2010 The PHP Group

All seems to work as expected.

<?php
// TestA.php
echo ' File A (1) ';
exec('C:\\php5\\php.exe -f Z:\\TestB.php > Z:\\output.txt');
echo ' File A (2) ';
?>

<?php
// TestB.php
echo 'File B';
?>

And because of my setup matching the docs at [1], I can actually use ...


<?php
echo ' File A (1) ';
exec('TestB > output.txt');
echo ' File A (2) ';
?>

as long as TestA.php and TestB.php are in the same directory.

So, initially, this looks like a non win32 issue (windows works - see !!!)

Regards,

Richard Quadling.

[1] http://docs.php.net/manual/en/install.windows.commandline.php
From: "Bob McConnell" on
From: Leonardo

> Em 15/07/2010 18:54, Shawn McKenzie escreveu:
>> On 07/15/2010 04:40 PM, Leonardo wrote:
>>>
>>> Bad habit. I know.
>>
>> Did it fix it?
>>
>
> Not really. The server allows short open tags. So, nothing changed.

You are running b.php as an external command, so it is running as a CLI,
not in the httpd server. You need to check to see how your PHP command
line is configured, it may need the full tag no matter how the server is
set up.

Bob McConnell
From: Jim Lucas on
Leonardo wrote:
> Hi everybody. I need to use exec() to run a background php script, but
> it's not working properly. Take a look at this sample:
>
> a.php
> <?
>
> echo ' File A (1) ';
>
> exec('php b.php > output.txt &');
>
> echo ' File A (2) ';
>
> ?>
>
> b.php
> <?
>
> echo 'File B';
>
> ?>
>
> output.txt (begins with 64 null bytes before the following)
> File A (2)
>
> I would expect the output's content to be "File B", but it's not
> happening. I came across this problem because I have a mailing
> application which stopped working after being moved to a new host. The
> messages were sent by a background script launched on user's demand.
> Like this:
>
> SendMessages.php
> <?
> ...
> exec('php Daemon.php -x ' . $USERID . ' > /dev/null &');
> ...
> ?>
>
> Now it won't work, and I got no clue about what's going on. What now?
>
> Thank you.
>

I tried running the same script, and found that the php binary is not in my path.

run this

echo passthru('which php');

Also, modify your existing exec() command to the following and it will capture
errors too.

exec('php b.php > output.txt 2>&1 &');

After running this is when I noticed it say "sh: php: not found"



--
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
From: Leonardo on
Em 16/07/2010 09:09, Richard Quadling escreveu:
>
> I'm on Windows XP SP3 and using
>
> PHP 5.3.3RC3 (cli) (built: Jul 15 2010 02:00:11)
> Copyright (c) 1997-2010 The PHP Group
>
> All seems to work as expected.
>
> <?php
> // TestA.php
> echo ' File A (1) ';
> exec('C:\\php5\\php.exe -f Z:\\TestB.php> Z:\\output.txt');
> echo ' File A (2) ';
> ?>
>
> <?php
> // TestB.php
> echo 'File B';
> ?>
>
> And because of my setup matching the docs at [1], I can actually use ...
>
>
> <?php
> echo ' File A (1) ';
> exec('TestB> output.txt');
> echo ' File A (2) ';
> ?>
>
> as long as TestA.php and TestB.php are in the same directory.
>
> So, initially, this looks like a non win32 issue (windows works - see !!!)
>
> Regards,
>
> Richard Quadling.
>
> [1] http://docs.php.net/manual/en/install.windows.commandline.php

Thanks for testing the code. I got it working now, as explained in my
reply to Bob's post.
From: Leonardo on
Em 16/07/2010 12:18, Jim Lucas escreveu:
>
> I tried running the same script, and found that the php binary is not in my path.
>
> run this
>
> echo passthru('which php');
>
> Also, modify your existing exec() command to the following and it will capture
> errors too.
>
> exec('php b.php> output.txt 2>&1&');
>
> After running this is when I noticed it say "sh: php: not found"
>
>
>

You're right about the path role in the issue. My code is working now,
and the solution is described in my reply to Bob's post.

Thank you.