Hello--

Is it possible to somehow keep php code running if the browser is turned off?

Like...maybe have a shell process to somehow keep it running?

Shell and php use almost the same code I believe...Would it be possible to run my php code as a process there? A script with loops in it...

Any ideas?

    I think that depends on what your doing.

    PHP scripts naturally times out. To keep it running forever, you can use set_time_limit(0) at the beginning, then have an infinite loop that calls a function over and over.

    If you say what you're trying to do, and the platform you're on, you'd probably get a better answer.

      I'm not sure about continually running the code, but CRON Jobs might be something to look into. They'd run the script whether a browser was running or not. Here's a short example:

      • User enters your site, and goes to "dynamic.php"
      • Dynamic.php calls upon a function to read an XML file from your server
      • User gets most up-to-date information in the XML file
      • User leaves site
      • Cron job runs a small script to rewrite the XML file every 5 minutes (extreme scenario)
      • User comes back 5 minutes later, XML file has changed content

      The cron would run automatically at times you specify. Not sure if that's what you're after. You might also look into running shell scripts from PHP. The downside is that the PHP page would still have to be called. And you'd need access to the server, which most shared hosting places won't give.

      ~Brett

        Cron jobs would work for what I'm trying to do but I actually want the script to run every ~11 seconds or so...

        It's a small script so I don't think that it will affect the server too much...

        The set_time_limit(0) code is what I think I'll need...

        I'm not sure how to make an infinite loop though.

        Here's what I have so far

        $x = "0";
        while ($x = "0") :
        
        CODE
        
        endwhile;
        

        I don't think this would work..?

        Thanks much!

          $x = 0;
          while($x < 10){
                    print "X equals $x and that is less than 10<br>";
                    $x++;
          }
          

          Im gonna test that one... And, I also have a sleep timer in between the loop btw...No worries 🙂

            Didnt work 🙁
            I let the the page load once, then closed the browser.

            <?
            set_time_limit(0);
            $x = 0;
            while($x < 10){
                      $fh = fopen("--", "r");
            while (!feof($fh)) :
            $data = fgetss($fh, 1000);
            endwhile;
            fclose($fh);
            sleep(11);
            }
            ?>
            

              PHP exists as a CLI program for both 'Nixes and Windows. I have CLI versions installed on all my UNIX servers, and my Win2K lappy.

              The PHP CLI does not time out during execution.

              I use it from cron for all kinds of stuff; it's great, because I can use the language for either interactive web scripting (in Apache) or for system administration.

              You need shell access to the box, of course. And, if you're going to run from shell, it'd be best to have a job control shell (don't know, can you do this in Windows????) to place the process in the bg. Best still to run from cron:

              #min    hr      day     mon     weekday         command
              */11    *      *      *           *             /path/to/php/program -q /path/to/my/script

              In Windows, you'd probably create a batch file that calls the php program and set the job up in "Task Scheduler" (C:\Windows\Tasks)....

              HTH,

                But, hrm ... and woopsie. Cron only runs once per minute, so you'd need some additional stuff. If it has to be every 11 seconds, it might be best to write it in "daemon" form (as above, with call to sleep() ...) and have it run from system startup. On 'Nix, at least, it wouldn't have to attach to any terminal.....

                  Well, I can easily create a background process on my laptop so that it accesses the site thus running the code. But, It would be MUCH better if it ran on some server...

                  You can run background processes on shell accounts, atleast, if you don't buy the cheapest one... They usually limit the number of processes you can run.

                  Anyway, I don't know how to use shell at all so its gonna be a problem to use it...
                  There are two ways in which the process can run i think...

                  1.) Call up a site, the site will have code in it to automatically refresh the page.

                  2.) Call up some code and run it, then run it a few seconds later again. (When I make it loop the code looses the affect)

                  I would prefer the first one if its possible but I dont think it is possible...

                  A question--

                  The script does something like this: opens a file then gets some data from it then prints it. The data it gathers isn't important. Whats important is what happens when it opens the file. It actually does have to do the printing. I tried to write the data to a database and a file but then the script lost its affect...

                  If I get a shell account, one allowing background processes then will I be able to run my script and have it work as it would on my computer? So that it does the Printing?
                  If so, can someone point me in a shell tutorial or something? One easily followed 🙂

                  Thanks much! If the script can run on a shell account, it would be very very nice!

                    I decided to use Cron.

                    And it works great! Thanks--

                      Write a Reply...