digushi
digushi

Reputation: 17

how run php program infinitely on my server

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.

Upvotes: 0

Views: 10418

Answers (6)

M3ghana
M3ghana

Reputation: 1281

You can try this -

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

Upvotes: 1

user1747935
user1747935

Reputation: 362

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

Upvotes: 0

niconoe
niconoe

Reputation: 1321

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...)

Upvotes: 5

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6204

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

Upvotes: 0

K..
K..

Reputation: 4233

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

Upvotes: 0

Luceos
Luceos

Reputation: 6730

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.

Upvotes: 4

Related Questions