php never ending loop

i am new to php, thanks you for your time :)

i need an function that excutes by it self in php whitout the help of corn.for that i have comeup with the following code which works for me well but as it is an never ending loop will it cause any problem to my server or script, if so give me some suggestion or alternatives. thanks.

$interval=60; //minutes

while (1){
    #do the routine job, trigger a php function and what not.
- - Source


answered 4 year ago MH2K9 #1

while(1) means it is infinite loop. If you want to break it you should use break by condition. eg,.

while (1){ //infinite loop
    #do the routine job, trigger a php function and what no.
    if(condition) break; //it will break when condition is true

answered 4 year ago Hyder B. #2

We have used the infinite loop in a live system environment to basically wait for incoming SMS and then process it. We found out that doing it this way makes the server resource intensive over time and had to restart the server in order to free up memory.

Another issue we encountered is when you execute a script with an infinite loop in your browser, even if you hit the stop button it will continue to run unless you restart Apache.

    while (1){ //infinite loop
    // write code to insert text to a file
    // The file size will still continue to grow 
    //even when you click 'stop' in your browser.

The solution is to run the PHP script as a deamon on the command line. Here's how:

nohup php myscript.php &

the & puts your process in the background.

Not only we found this method to be less memory intensive but you can also kill it without restarting apache by running the following command :

kill processid

Edit: As Dagon pointed out, this is not really the true way of running PHP as a 'Daemon' but using the nohup command can be considered as the poor man's way of running a process as a daemon.

answered 4 year ago loki9 #3

You can use time_sleep_until() function. It will return TRUE OR FALSE

$interval=60; //minutes
  set_time_limit( 0 );
  $sleep = $interval*60-(time());

  while ( 1 ){
     if(time() != $sleep) {
       // the looping will pause on the specific time it was set to sleep
       // it will loop again once it finish sleeping.
     #do the routine job, trigger a php function and what not.

answered 4 year ago Amgine #4

There are many ways to create a daemon in php, and have been for a very long time.

Just running something in background isn't good. If it tries to print something and the console is closed, for example, the program dies.

One method I have used on linux is pcntl_fork() in a php-cli script, which basically splits your script into two PIDs. Have the parent process kill itself, and have the child process fork itself again. Again have the parent process kill itself. The child process will now be completely divorced and can happily hang out in background doing whatever you want it to do.

$i = 0;
    $pid = pcntl_fork();
    if( $pid == -1 ){
        die( "Could not fork, exiting.\n" );
    }else if ( $pid != 0 ){
        // We are the parent
        die( "Level $i forking worked, exiting.\n" );
        // We are the child.
}while( $i < 2 );

// This is the daemon child, do your thing here.

Unfortunately, this model has no way to restart itself if it crashes, or if the server is rebooted. (This can be resolved through creativity, but...)

To get the robustness of respawning, try an Upstart script (if you are on Ubuntu.) Here is a tutorial - but I have not yet tried this method.

comments powered by Disqus