Page 1 of 1

Can I Make A PHP Script Run FOREVER? OR Meta-Refresh maybe..

Posted: Thu Jan 30, 2003 7:40 am
by superwormy
I have a PHP script that I want to run FOREVER. IE, never end, either that OR I want it to refresh after it runs each time.

My original thought was to set an HTML meta-refresh tag, HOWEVER, I'd like to run this on Unix using Lynx, but Lynx does not follow meta-refresh tags ( does anyone know how to make it follow meta-refresh? )

It's been suggested I use Cron to schedule it, but theres no way to know how long the script will take to run, ie sometimes it takes 20 seconds, sometimes it takes 2 seconds.

It just runs queries on a database, and I'd like to have it run for MONTHS at a time, does nayone have any ideas? Can I turn off the script execution time limit? Will PHP / Apache crash if I do this and let it run for 3 months at a time?

Posted: Thu Jan 30, 2003 8:11 am
by Kriek
Personally I would schedule a Cron job for this, however you can use the header() function to refresh/redirect the page and the sleep() function to delay the script. To be sure that you aren't sending anything to the browser prior to the header() command use output buffering with ob_start() and ob_end_flush() -- although ob_end_flush() isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start()

Code: Select all

<?php ob_start(); ?>

<?php 
// your page, script, or code here
sleep( '5' );
header("Location: http://$SERVER_NAME/page.php"); ?> 

<?php ob_end_flush(); ?>
Or perhaps you can attempt this method.

Code: Select all

<?php ob_start(); ?>

<?php 
// your page, script, or code here
header("refresh: 5; url=http://$SERVER_NAME/page.php"); ?>

<?php ob_end_flush(); ?>

Posted: Thu Jan 30, 2003 8:46 am
by superwormy
Hey... ya know... that just might work... damn...

Thats a good idea, thanks :-)

Posted: Thu Jan 30, 2003 11:51 am
by Stoker
If you don't need the result over http you could just run it on the server as a 'daemon' using the php-cgi executable? Just make a never ending loop with a sleep() inside..