10
<?php
 while(true){
 //code goes here.....
 }
  ?>

I want to make a PHP web server, so how can I make this script run forever with Curl?

cdxf
  • 5,501
  • 11
  • 50
  • 65
  • 1
    what you need is `while(true)`, and yes it will keep running forever until something happens (preferably due to a breaking condition you specify) – Adi Jun 21 '12 at 06:38
  • @AdnanShammout it will terminate automatically after sometime. PHP has a set max execution time built-in, unless you modify that setting in your code. – Dexter Huinda Jun 21 '12 at 06:56
  • @DexterHuinda, PHP execution limit only applies when running from a web browser. When running as a command `php server.php` the limit doesn't apply. Why would anyone want to run a server forever from a web browser? – Adi Jun 21 '12 at 06:57
  • @AdnanShammout An embedded java program, applet can run inside a browser, forever, and you have an administration interface directly available via the browser. Most people cannot afford hosting in servers where you can access the shell directly, so a browser-based implementation is an option. – Dexter Huinda Jun 21 '12 at 07:11
  • It should be noted that this will consume the CPU, as `while(true)` makes the CPU executes instructions e.g: `cmp` forever. Consider sleeping inside the loop if there is no *real* work to do. – Accountant م May 18 '19 at 02:31

4 Answers4

23

Don't forget to set maximum execution time to infinite(0).

Better make sure you don't run more than one instance, if that's your intention:

ignore_user_abort(true);//if caller closes the connection (if initiating with cURL from another PHP, this allows you to end the calling PHP script without ending this one)
set_time_limit(0);

$hLock=fopen(__FILE__.".lock", "w+");
if(!flock($hLock, LOCK_EX | LOCK_NB))
    die("Already running. Exiting...");

while(true)
{

    //avoid CPU exhaustion, adjust as necessary
    usleep(2000);//0.002 seconds
}

flock($hLock, LOCK_UN);
fclose($hLock);
unlink(__FILE__.".lock");

If in CLI mode, just run the file.

If in another PHP on a webserver, you could start the one which must run infinetely like this (instead of using cURL, this eliminating a dependency):

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);

Or you could start from linux cron using wget like this:

`* * * * * wget -O - http://localhost/infinite_loop.php`

Or you could start from Windows Scheduler using bitsadmin running a .bat file which contains this:

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
oxygen
  • 5,891
  • 6
  • 37
  • 69
  • added ignore_user_abort to make sure script doesn't end because caller abandonded the request, when in a web environment. – oxygen Jun 21 '12 at 07:40
  • @Tiberiu-IonuțStan, so how do you actually terminate the request when you need to do so? – Pacerier Jul 13 '13 at 10:09
  • @Tiberiu-IonuțStan, what do you mean? – Pacerier Jul 13 '13 at 10:40
  • 3
    @pacerier As a programmer, you will foresee the need to auto-terminate based on certain conditions or maybe even external flags, and put that inside the beeing run code (insinde infinite_loop.php). – oxygen Jul 13 '13 at 10:45
  • 1
    @Tiberiu-IonuțStan, yes but there will always be bugs. Do you mean that we have *no* way to terminate a rouge script then? – Pacerier Jul 13 '13 at 10:46
  • @Parcerier Do you mean that you have just *dismissed* the solution I just gave you? – oxygen Jul 13 '13 at 10:50
  • If I triggered the PHP script using by an http request `wget` ot `file_get_conteents`, Doesn't Apache [timeout-directive](https://stackoverflow.com/questions/9629566/how-to-increase-apache-timeout-directive-in-htaccess) control the script timeout ? – Accountant م May 18 '19 at 02:24
3

For a php code to run forever, it should have the ff.:

  • set_time_limit(0); // so php won't terminate as normal, if you will be doing stuff that will take a very long processing time
  • handler for keeping the page active [usually by setting up a client-side script to call the same page in intervals] See setInterval(), setTimeout()

EDIT: But since you will be setting up a cron job then you can keep away from the client-side handling.

EDIT: My suggestion, is not to use infinite loops unless you have a code that tells it to exit the loop after some time. Remember, you will be calling the same page using a cron job, so there is no point in keeping the loop infinite. [edit] Otherwise, you will be needing a locking system as suggested by @Tiberiu-Ionuț Stan so only 1 instance may run each time the cron job is called.

Dexter Huinda
  • 1,242
  • 1
  • 7
  • 9
1

By default, no because PHP has an execution time limit. See: http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

You can make it run forever by either setting the value or call set_time_limit in your script (http://php.net/manual/en/function.set-time-limit.php).

But I don't recommend this because PHP (called by HTTP request) is not designed to have an infinite loop. Use a local script instead if you can, or request the page in intervals to do the task frequently.

If your website is browsed by others frequently you can do this in every page instead.

(And imagine if someone requests the script more than once, you will have multiple instance of it running)

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
  • This only applies only when running from a web browser – Adi Jun 21 '12 at 06:39
  • @AdnanShammout He stated `curl` so the best guess will be running from HTTP request – Alvin Wong Jun 21 '12 at 06:53
  • Would you please guide me through that inference process? I don't see how `curl` would indicate running from a web browser. – Adi Jun 21 '12 at 06:56
  • @AdnanShammout my bad, I should define HTTP request instead of "web browser" – Alvin Wong Jun 21 '12 at 06:57
  • 2
    Again, I don't see how using `curl` means you're running from an HTTP request! Are you sure you know how `curl` works? Curl will help you make HTTP request INSIDE your script, it doesn't have anything to do with how you run that script (HTTP request or command line) – Adi Jun 21 '12 at 07:00
  • @AdnanShammout In my interpretation "will this script run forever with Curl" implies that he is calling the script with `curl`, not calling `curl` in the script. And there is no point to call `curl` in PHP because there is other functions to do that – Alvin Wong Jun 21 '12 at 07:03
  • See my answer about how to launch ur infinite script in different ways. – oxygen Jun 21 '12 at 07:46
0

You can make it happen only if you set set_time_limit(0) in your script, else it will stop execution after max_execution_time set in configuration.

And you are using while(true) condition, that will make your script to run always.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101