SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Periodically running a PHP script

    I need some PHP script (that gets feeds) to run periodically on this site I'm supposed to make. How is this done usually? This needs to be set up on an ordinary LAMP hosting service. How would you do this?

    One way that comes to mind is simply checking the time when someone visits the site and triggering the script but this seems very inelegant.

    How is this kind of thing done usually? Is cron available on most hosting services?

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're running it on Windows, use the AT command. It works similarly to cron.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    If you're running it on Windows, use the AT command. It works similarly to cron.
    Nope, it's going to run on a hosting service (LAMP) but now I've seen that my client's host doesn't allow cron jobs.

  4. #4
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by A-OK View Post
    Nope, it's going to run on a hosting service (LAMP) but now I've seen that my client's host doesn't allow cron jobs.
    Well, cron is the elegant solution. You either need to discuss switching hosts or inform them that you're going to have to do something dirty.

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either move to a new host that supports cron jobs, or, I had an idea for doing something similar once. Store the date of each person that visits your site. Then, the closest match to when you would run your cron job (say, each day) would trigger a script to include the cron job. Now, it's a bit hacky, I'm not even sure if it is technically feisable, but, a couple of years ago, when I was just thinking up of some really odd solutions without finding out the proper way, it's something I came up with. I never tried it, though.

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a variation on Adam's solution above.

    Store the time of day that you want the script to run. Each time someone views a page, check whether that time has passed already. If so, run the "cron" script after sending the response to the visitor's request. Nobody's going to care whether the application data is up-to-date until someone actually views a page, right?

    Caveats:

    You will have to send a Content-Length header to make the visitor's browser disconnect once the response is sent, otherwise they will sit there with an hourglass cursor until the script ends.

    You will also have to store a value indicating that the "cron" script has been run for that day so that it doesn't get run repeatedly after the desired run time has passed.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks guys. The best thing to do is to change the hosting service since it's a new site and the only loss will be the remaining 8 months of the current hosting plan.

    Btw., WWWeird useful stuff about the Content-length response, that'll come in handy on this other job I'm doing.


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
  •