1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Automatic execution of PHP script

Discussion in 'PHP' started by vnviews, Jun 22, 2006.

  1. #1
    Can anyone help me with this:
    I want to execute specific PHP script at the specific time of day automatically. Let say: I want to execute myscript() at 8 P.M. every day (for automatic maintenance purpose). Can be it done by PHP?
    Thanks in advance.
    Tuan.
     
    vnviews, Jun 22, 2006 IP
  2. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP doesn't have any scheduling features. You'll have to talk to your host to find out if the'll let you use cron or whatever schedule event thing they have.

    You can also schedule a task on your computer to run the script from your website as well.

    -the mole
     
    themole, Jun 22, 2006 IP
  3. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Some hosting control panel gives you options to set your cron timings from your hosting account. Just check up.
     
    smo, Jun 23, 2006 IP
  4. balamm

    balamm Peon

    Messages:
    64
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you have no cron but you do have a busy web page, you could do something like embed a small script in that page.
    <?php
    
     $t = date(G);
      if($t =='20'){
       /* DO SOMETHING IF IT'S BETWEEN 8PM AND 9 PM */
      }
    
    
     $t = date(Gi);
      if($t =='2000'){
       /* DO SOMETHING IF IT'S BETWEEN 8:00 AND 8:01 */
      }
    
    
     $t = date(Gis);
      if($t =='200000'){
       /* DO SOMETHING IF IT'S EXACTLY 8:00 */
      }
    
    ?>
    PHP:
    Of course you'd need to script a lot more into it to prevent it running more than once for the first 2 options but it's a start. ;)
     
    balamm, Jun 23, 2006 IP
  5. bmelton

    bmelton Peon

    Messages:
    69
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I was going to suggest the same exact thing balamm. One thing you could do to prevent it running repeatedly is to create a directory named with the day-month-year. If that directory doesn't exist, then go ahead and run the script, otherwise, do nothing.

    Make sure that it deletes yesterday's directory though, or you could run out of space over time.

    Also, as mentioned, if you have access to Cron, use that. Any PHP implementation is going to be a LOT of overhead that you just don't need. I recommend dumping it into an include file at the END of the page so that it at least doesn't drag things down between 8 and 9.

    Lastly, it's worth noting that the PHP script balamm posted will do absolutely nothing if you don't get any users between 8 and 9pm.
     
    bmelton, Jun 24, 2006 IP
  6. vnviews

    vnviews Peon

    Messages:
    746
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks, guys.
    Yes, my problem is:
    - no cron function
    - new web site, that I can't expect that always have some user between the time.
    I just implemented similar script, but it seems that I have to visit my site one time per day :( for maintenance purpose, at least at the begin until I have more visitors.
    Once, thanks.
     
    vnviews, Jun 25, 2006 IP
  7. bmelton

    bmelton Peon

    Messages:
    69
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Depending on what it's doing, or how time-sensitive, if you set the "start time" to midnight, and create a time-stamped directory when the task begins, you can do away with the end range, effectively eliminating someone having to be there between a given time range.

    In that scenario, you'd be good so long as one person visits the site per day.

    If it is something fairly critical, I would recommend switching hosts to one that does provide cronjobs. I use Dreamhost, and know that they provide access to per-user crontab.
     
    bmelton, Jun 25, 2006 IP
  8. FxAnd

    FxAnd Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ya agree with bmelton..
    recommend switching hosts to one that does provide cronjobs
    if your server side doesnt provide the cronjobs then will screw up..
    cronjobs is compulsory for advance server. ok.
     
    FxAnd, Jun 26, 2006 IP
  9. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #9
    if you have no traffic yet, and no cron-jobs available, why not setup a script that does your functions that can only execute if the correct username and password querystring's are are passed in. Then build an app that hits that page at your requested interval. Or... (and this is not the slickest way) build a Content-Refresh in your meta on the page. Have it refresh ever - whatever, 100000 seconds or something, then just leave this page open. It will refresh automatically if you leave the webpage open at all times.
     
    ccoonen, Jun 28, 2006 IP
  10. bmelton

    bmelton Peon

    Messages:
    69
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just remember the catch with that is that if your connection blinks for a second, or the server isn't available one time, then it's going to refresh to a 404 and the cycle's dead from that point forward.
     
    bmelton, Jun 28, 2006 IP
  11. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #11
    exactly, I would actually advise developing an easy app, sits in systray, and hits X url and collects Status Returned until traffic starts coming in... This way you could set the interval to whatever you want and it will always hit the page you request it to hit. Actually, you might be able to do this with a DTS Too... or hmm, does Apache DB Flavors have DTS's? I know MySQL 5 has some big advancements... but not exactly sure on what.
     
    ccoonen, Jun 28, 2006 IP
  12. webbist

    webbist Peon

    Messages:
    89
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Cronjobs are the surefire way to schedule PHP execution. Most control panels support cron jobs. Something I've noticed recently, as I've been using crons myself quite often, is using a wget command, then the URL to your PHP script works better then executing it as a path. Some of my scripts wouldn't work when I executed it as a path, but I contacted my sysadmin and he set it up so it executed via URL with wget.
     
    webbist, Jun 28, 2006 IP
  13. dzlalov

    dzlalov Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #13
    there are at least dozen of web services out there offering automatic script execution of web scripts in predefined
    time intervals. some of them even log your script output.
    I have used two of them:
    getcron.com - best feature: timezone support
    webcron.org - best feature: sms notifications

    both have logs and big timeouts.
    no need to learn cron commands - just paste the link and using a very easy to use web interface just set the days and hours.
     
    dzlalov, Feb 18, 2009 IP
  14. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #14
    Another thing to try is www.setcronjob.com
    Reliable and affordable :D
     
    thuankkk, Jul 20, 2009 IP