views:

45

answers:

4

I want to run a PHP file/function that will not stop until it finishes it work. Is there any option for that?

(It will not print any information to the browser, it will make calculations and it will store them on a database.

Note: The Apache server is in my PC.

+3  A: 

PHP typically has an execute time limit, and long running requests will hold onto the HTTP connection.

You will want to add a job to a batch queue and have some worker pick-up the work outside of the HTTP request (via a Cron job perhaps).

Here is a good tutorial by IBM that details the cron method complete with MySQL tables and PHP code. Other options are Messaging/Queuing systems rather than a database.

Aiden Bell
+3  A: 

Check out, http://php.net/manual/en/function.ignore-user-abort.php

however if apache is restarted it will be reset.

Also check out the max execution time as well

Wizzard
+1 - Handy to know
Aiden Bell
A: 

If you're running on Windows (assuming you are), you can use the WindowsShell object to exec the script. This will run the script in the background, without opening a command window on your desktop.

$shell = new COM("WScript.Shell"); 
$cmd = $WshShell->Run("c:\php\cli\php.exe c:\yourscript.php", 0, false);

Under the CLI version of PHP, scripts have no max execution time.

If you just need to run a PHP script and don't care about going through a web browser to get there, just run the script directly, using the CLI version of PHP.

If you're on a UNIX system, use exec(). Same idea.

Jack Shedd
+1  A: 

put this statement at the top of the script: set_time_limit(0);

This will make the script run indefinitely.

BugSquasher