SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    make a script run forever

    hello,
    i have a question. i would like a page to reload itself. or rather simulate it. it's just a server-side script. until now i've used the following solution: a little jscript reloads the page and the script runs on reload. it occurs every 30s. problem: if i close my browser, the script won't run until i re-open the page in a new window.

    how could i workaround this?

    i thought about an infinite loop with a sleep of 30s between each cycle. does it sound good? in other words, how could i execute a script every 30s?

    thanks in advance

  2. #2
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you will need to create a cron job to run your script.

    This will call your script by the time schedule you specify. You will probably need root access to a/your server or have a good controlpanel such as plesk.

    http://www.interspire.com/whitepaper...uction_to_Cron

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I knew about Cron... forgot to mention that I couldn't use this solution... But I could ask my host to change the default timeout script and run the loop with the sleep forever.
    any other ideas maybe?

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah! A psuedo cron! Won't be every thirty seconds though.

  5. #5
    SitePoint Addict phpster's Avatar
    Join Date
    Feb 2005
    Location
    Toronto, Canada
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could set up a cron job to call that page from your home machine...
    phpster

    I wish my computer would do what I want it to.
    Not what I tell it to do...

  6. #6
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And how could I set up a pseudo cron?
    is the infinite loop a bad idea?

  7. #7
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. http://www.bitfolge.de/pseudocron-en.html (i know nothing about this so cant help in anyway.. google is useful though.. !)

    2. Yes.

  8. #8
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah ok... now I see what a pseudo-cron is. Definitly not what I was looking for. Read my first post: every 30s a script must be run.
    Quote Originally Posted by Will Kelly
    2. Yes.
    care to explain why?

  9. #9
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while (==1) {
        
    // I never stop

    Infinite loops like that put a lot of strain on Apache (in my experience)

    If I was going to try anything, I would try this: (First create a file called kill.txt, and change the value to 'y' if you want it to stop running, because this will run forever, even if you close your browser)
    PHP Code:
    <?php
    ignore_user_abort
    (true);
    set_time_limit(0);
    while (
    == 1) {
        if (
    strtolower(file_get_contents('kill.txt')) == 'y') {
            exit;
        }

        
    // Your code

        
    sleep(30);
    }
    ?>
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  10. #10
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so Apache wouldn't be quite happy and I'm left with a cron job as my only option, which is something I don't have access to... great....
    could a loop like this one be executed without having Apache to kill the script after 30s?

  11. #11
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can I ask why you have a script that needs to run so often. Perhaps if we knew about the problem a bit more, someone could come up with another way of doing it !
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  12. #12
    phpLD Fanatic bronze trophy dvduval's Avatar
    Join Date
    Mar 2002
    Location
    Silicon Valley
    Posts
    3,626
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I've also seen a method that uses javascript to keep refreshing the page before timeout.

  13. #13
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dvduval
    I've also seen a method that uses javascript to keep refreshing the page before timeout.
    JS runs on the browser, not the server, so once the browser is closed it wouldn't recall the script anymore
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  14. #14
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes
    JS runs on the browser, not the server, so once the browser is closed it wouldn't recall the script anymore
    thus my problem

    I need to send data to an external website every 30s.

  15. #15
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I need to send data to an external website every 30s.
    What, even when you have no traffic yourself ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  16. #16
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exactly...
    But I really think that it's either browser + JS, or cron jobs. No ther way beside the infinite loop.

  17. #17
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Psuedo cron wins here. If you have a psuedo cron you will only run your script if the site has been changed or viewed. Only reason I can think of for sending regular and unchanged info is just to prove a site is working.

  18. #18
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a project that needs to send data to an external application every 30s. No matter what. I can't wait that a user connects to my website. There is not even visible pages. It's a website whose sole purpose is to communicate with an external app. Users are not to worry about, the address won't even be known and the server will block every IP but the one of the external app.

    What about Process Control Functions? could it be of any use?

  19. #19
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    It's a project that needs to send data to an external application every 30s.
    Probably no but I'll ask anyway, is there no question of the external app contacting the website every 30s to establish the conection ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  20. #20
    SitePoint Addict phpster's Avatar
    Join Date
    Feb 2005
    Location
    Toronto, Canada
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why can'y you use cron? you mentioned you couldn't but didn't give a reason why...
    phpster

    I wish my computer would do what I want it to.
    Not what I tell it to do...

  21. #21
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @phpster: Do I have to give you a reason? Well strange... but here you go: my host doesn't allow it.

    @Mandes: Unfortunately, the external app doesn't behave to connect to the mutiplicity of apps supposed to get connected to it. It just handles connection.

    Is anyone familiar with Process Control Functions?

  22. #22
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would consider getting a host that does support it if it is such an important function. There must be loads of hosts that do support it without having to have your own server (I know 1&1 here in the UK.. well ok germany, support it on pro accounts).

    And there is no way that the external app can invoke the php script every thirty seconds (and read in the response)?

  23. #23
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd use javascript to refresh the page every 30 seconds. Doing it server side would take bog down apache.

  24. #24
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gator99
    You'd use javascript to refresh the page every 30 seconds. Doing it server side would take bog down apache.
    I thought that so I tested to find out and it is not true. While the script is 'sleeping' CPU activity drops to zero.

  25. #25
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Something that runs every 30 seconds would use a big chunk of the available resources on the server and slow down any other sites hosted there. If you want something that runs that frequently you probably need a dedicated server to run it on. With as dedicated server you decide what access you want to have available and so will be able to set up a cron job.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •