By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,349 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,349 IT Pros & Developers. It's quick & easy.

PHP, cron and stdout redirection

P: n/a
I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
32bit. Php does run as an Apache SO.

It works perfectly if I just hit the page in a web-browser (output to
browser).

It works *perfectly* from the command line (dumps the text to the file
per the redirect)
/usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
dashboard/dash.inc

When run as cron, it updates the timestamp of the target file, but
does not write the text, nor any error.
05,15,25,35,45,55 * * * * /usr/bin/php -q /var/www/html/dashboard/
dash1.php /var/www/html/dashboard/dash.inc

There is nothing in php error log, syslog or any other location I can
find. The entry in cron log suggests that it works perfectly. It does
not email any errors to me.

I'm very puzzled.... anyone have any idea for trouble shooting ????

philc
(this is not hosted, I control/operate the linux server)
Feb 20 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Phil wrote:
I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
32bit. Php does run as an Apache SO.

It works perfectly if I just hit the page in a web-browser (output to
browser).

It works *perfectly* from the command line (dumps the text to the file
per the redirect)
/usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
dashboard/dash.inc

When run as cron,
Whose cron? with what permissions?

>it updates the timestamp of the target file, but
does not write the text, nor any error.
05,15,25,35,45,55 * * * * /usr/bin/php -q /var/www/html/dashboard/
dash1.php /var/www/html/dashboard/dash.inc

There is nothing in php error log, syslog or any other location I can
find. The entry in cron log suggests that it works perfectly. It does
not email any errors to me.

I'm very puzzled.... anyone have any idea for trouble shooting ????
Work out what effective user cron is running the script at, 'su -' to
that user, and run the exact command line from there.
Or try setting the cron entry to write to a brand new file in /tmp,
which is world writable, and see what permissions it turns up with.

and indeed whether it works...at all.
philc
(this is not hosted, I control/operate the linux server)
Feb 20 '08 #2

P: n/a
Greetings, Phil.
In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
32bit. Php does run as an Apache SO.
It works perfectly if I just hit the page in a web-browser (output to
browser).
It works *perfectly* from the command line (dumps the text to the file
per the redirect)
/usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
dashboard/dash.inc
When run as cron, it updates the timestamp of the target file, but
does not write the text, nor any error.
05,15,25,35,45,55 * * * * /usr/bin/php -q /var/www/html/dashboard/
dash1.php >/var/www/html/dashboard/dash.inc
There is nothing in php error log, syslog or any other location I can
find. The entry in cron log suggests that it works perfectly. It does
not email any errors to me.
I'm very puzzled.... anyone have any idea for trouble shooting ????
I think Your problem is redirection in cron job. You can't be sure what is
redirected to the file.
Redone it as follows:
<?php

ob_start();
include_once('/var/www/html/dashboard/dash1.php');
$rc = ob_get_contents();
ob_end_clean();

$f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
fwrite($f, $rc);
fclose($f);

?>
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 20 '08 #3

P: n/a
On Feb 20, 8:42*am, AnrDaemon <anrdae...@freemail.ruwrote:
Greetings, Phil.
In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
32bit. Php does run as an Apache SO.
It works perfectly if I just hit the page in a web-browser (output to
browser).
It works *perfectly* from the command line (dumps the text to the file
per the redirect)
* */usr/bin/php /var/www/html/dashboard/dash1.php */var/www/html/
dashboard/dash.inc
When run as cron, it updates the timestamp of the target file, but
does not write the text, nor any error.
* * 05,15,25,35,45,55 * * * * /usr/bin/php -q */var/www/html/dashboard/

dash1.php *>/var/www/html/dashboard/dash.inc
There is nothing in php error log, syslog or any other location I can
find. The entry in cron log suggests that it works perfectly. It does
not email any errors to me.
I'm very puzzled.... anyone have any idea for trouble shooting ????

I think Your problem is redirection in cron job. You can't be sure what is
redirected to the file.
Redone it as follows:

<?php

ob_start();
include_once('/var/www/html/dashboard/dash1.php');
$rc = ob_get_contents();
ob_end_clean();

$f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
fwrite($f, $rc);
fclose($f);

?>

--
Sincerely Yours, AnrDaemon <anrdae...@freemail.ru>
I might try that ... the work around that I already implemented was to
use a wget <url-O <output filein cron. Seemed kinda hack-ish, but
it did work :-)

I suspect the root cause is related to the environment, since that is
also a somewhat common problem that unix people have with other cron
scripts (shell, perl, python, etc), but not sure how to overcome it
for php in cron. Anyway, it was a rather unusual situation were I
wanted to use the same code for a live-page with DB lookups, and for
an cron-scripted output to a page for the same DB lookups.

Thanks All. Unless anyone a further brain-storm about the env for cron
php, I think this is a closed issue.
Feb 22 '08 #4

P: n/a
Phil wrote:
On Feb 20, 8:42 am, AnrDaemon <anrdae...@freemail.ruwrote:
>Greetings, Phil.
In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
>>I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
32bit. Php does run as an Apache SO.
It works perfectly if I just hit the page in a web-browser (output to
browser).
It works *perfectly* from the command line (dumps the text to the file
per the redirect)
/usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
dashboard/dash.inc
When run as cron, it updates the timestamp of the target file, but
does not write the text, nor any error.
05,15,25,35,45,55 * * * * /usr/bin/php -q /var/www/html/dashboard/
dash1.php >/var/www/html/dashboard/dash.inc
>>There is nothing in php error log, syslog or any other location I can
find. The entry in cron log suggests that it works perfectly. It does
not email any errors to me.
I'm very puzzled.... anyone have any idea for trouble shooting ????
I think Your problem is redirection in cron job. You can't be sure what is
redirected to the file.
Redone it as follows:

<?php

ob_start();
include_once('/var/www/html/dashboard/dash1.php');
$rc = ob_get_contents();
ob_end_clean();

$f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
fwrite($f, $rc);
fclose($f);

?>

--
Sincerely Yours, AnrDaemon <anrdae...@freemail.ru>

I might try that ... the work around that I already implemented was to
use a wget <url-O <output filein cron. Seemed kinda hack-ish, but
it did work :-)

I suspect the root cause is related to the environment, since that is
also a somewhat common problem that unix people have with other cron
scripts (shell, perl, python, etc), but not sure how to overcome it
for php in cron. Anyway, it was a rather unusual situation were I
wanted to use the same code for a live-page with DB lookups, and for
an cron-scripted output to a page for the same DB lookups.

Thanks All. Unless anyone a further brain-storm about the env for cron
php, I think this is a closed issue.

I have a sneaking suspicion it may be due to the way PHP does its
buffering. The fact that a file is CREATED means there are no perm problems.
Not sure what the actual PHP commands are, but the equivalent of an
fflush();fclose();sync(); on stdout is something I would try..
Feb 23 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.