0

I have written php program and uploaded on server. I want run this program infinitely. My programs source is like this:

<?php
while (1<2){
make something;
}
?>

Of course, if i will open this page in my browser it will run, but if i will shut down my pc it will stop working. How i can run this program infinitely without opening in any browser.

3
  • In *nix <?php if (isset($_SERVER['HTTP_HOST'])) { exec("php ".__FILE__." > /dev/null 2>&1 &"); exit; } // Do stuff here. In Windows it is too long winded for a comment. Basically, when called through a browser, exec() yourself and release the association with the new process. Commented Jun 28, 2012 at 13:05
  • 1
    PHP is not really designed to run in an endless loop. On most servers there's a max execution time (defailt is 30 seconds) that will assume a script that's still running after that point has malfunctioned and shut it down. Also, your technique will cause 100% CPU usage on one core because it will execute a calculation in an endless loop. You might want to look at a different technology for the bits of your app that need to run like this (node.js maybe) and you need to use a different approach from busywaiting (maybe sleep()) Commented Jun 28, 2012 at 13:14
  • why you want to run script infinitely? I guess you want to check something constantly right? Commented Jun 28, 2012 at 13:46

6 Answers 6

5

Do this:

<?php
    set_time_limit(0);
    ignore_user_abort(true);
    while(true) {
        //Do something
    }
?>

But it's a very very very bad idea to do that without a very very very good reason. You might run that kind of script in CLI and use a SIGINT or a SIGKILL to be sure stopping your script without rebooting your apache server... (Why I just explain that? Don't do it man, it's dangerous...)

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know if I should DV for telling him how to do it or UV for telling him why he shouldn't.
4

Run in commandline or run as a cronjob; you can also check for making a php file a system daemon:

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

Using PHP as a daemon allows you to make it run indefinitely, however you might have to reset it at regular intervals to ensure it does not stack memory.

By the way:

while( true )

does also work.

2 Comments

A while(true) loop is a REALLY bad idea, even if you spend some time in the loop sleep()ing. PHP just isn't meant for this type of job, and rather than approaching the problem as if it were a nail you should find a more suitable tool than the PHP hammer.
@GordonM I know, but it's the same as 1<2 and I'm not advising him on what is better alternative, I'm just giving him an answer.
1

You can try this -

<?php
    while(1) {
        "some code"
    }
?>

Comments

0

you could start the script with popen() which starts a new commandline process. So you would start a CLI PHP with the desired script.

Comments

0

You can use deamons (service), must run script. description here

Comments

0

I encountered this same problem for running a java program infinitely on a linux server.

I solved the problem by using the linux 'screen' command, instructions found here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.