Every half hour script

This is a discussion on "Every half hour script" within the PHP Forum section. This forum, and the thread "Every half hour script are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 17th, 2007, 22:19
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Every half hour script

Hi. I have a problem that I don't know how to solve.

I need a script that will run and update some info in a DB table every half an hour.

The script itself shouldn't be to hard it's just I don't know how to make it run every half an hour.

Is there a way to make the script run without a user being there to refresh that page? Like make the server do it or something. I really can't figure out how this will work.

Thanks for your help

Blake
Reply With Quote

  #2 (permalink)  
Old Aug 17th, 2007, 22:29
Junior Member
Join Date: Aug 2007
Location: London
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Every half hour script

Do you have either a dedicated server or VPS, or just a basic shared server?
Reply With Quote
  #3 (permalink)  
Old Aug 17th, 2007, 22:30
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: Every half hour script

basic shared server i'm working on.

It supports cron jobs though. If it makes a difference at all.
Reply With Quote
  #4 (permalink)  
Old Aug 17th, 2007, 23:57
Junior Member
Join Date: Aug 2007
Location: London
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Every half hour script

All the difference as in that case it should be easy. If you have access to a personal raw crontab file, simply add a line such as:

Code: Select all
0,30 * * * * php yourscript.php
You may need to specify a full path for the php binary and a suitable path for your script, but that's the essence of what you'd add to the crontab file. In this case it says run at 0 and 30 mins past each hour, every day, month and year. Other forms of syntax include /n to specify a repeat interval, e.g. */5, and ranges, such as 20-50 in the first field to mean every minute between 20 minutes and 50 minutes past the hour.

Without cronjobs, the trick is to hook a script into a core file of your website that gets hit on every request, record when your scheduled script was last run, and if the next trigger time has passed then execute the script as part of the page request. As long as the site gets a hit at least every minute or so then you'll get a fairly or even completely accurate scheduler that way. The slightly tricky thing with this method is that it's important to avoid race conditions in some way, such as testing that the cronjob needs running, then getting a lock (e.g. a file lock with flock()), testing again, and if it still needs running then update the time that it last ran, release the lock, and then do the work.

Last edited by ioncube; Aug 18th, 2007 at 00:01.
Reply With Quote
  #5 (permalink)  
Old Aug 18th, 2007, 10:15
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: Every half hour script

Thanks a lot!

I'm looking into that right now
Reply With Quote
  #6 (permalink)  
Old Aug 18th, 2007, 10:29
Junior Member
Join Date: Aug 2007
Location: London
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Every half hour script

You should be able to use environment variables in the crontab too, e.g.

Code: Select all
* * * * * $HOME/bin/somescript.sh
would run somescript.sh every minute in your account root/bin directory. This would avoid hardcoding your account directory location. You might need to ask your host or look in their KB about how to set it up though as giving the ability to run programs on a shared server is a potential security risk, and there might be limitations on what programs you can use, although you just need to run PHP cli and that should be fine.
Reply With Quote
Reply

Tags
half hour, no users, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 Hour Website shawzy85 Free Web Site Critique 22 Jul 9th, 2008 19:24
4 hour Website shawzy85 Free Web Site Critique 8 Apr 28th, 2008 22:03
A job half done geordief Flash & Multimedia Forum 12 Sep 10th, 2007 22:32
only 'half' border appears in firefox N_K Web Page Design 4 Aug 2nd, 2006 19:10
Half Project Smokie Entry, Nominations and Voting 0 Aug 19th, 2003 11:58


All times are GMT. The time now is 05:46.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43