Click to See Complete Forum and Search --> : Run server-side scripts automatically?
eventide
08-23-2011, 09:00 AM
I have a potential client that wishes to has some stuff done to his database automatically based on timed information. I.e. a record in a database table will have a column titled "endDateTime", for instance. And when that date/time happens, I need to automatically update some information in the database.
I believe doing something like that would require setting up a cron job, but I have absolutely no experience with cron jobs and when I did look into them in cPanel for my own site I was thoroughly lost!
I'd appreciate it if anyone can provide some suggestions on how to accomplish what I'm describing and/or some resources that I could visit/use.
Thanks!
aj_nsc
08-23-2011, 09:21 AM
I'm not particularly a fan of cron jobs, I don't necessarily have a vendetta against them or anything, I just find I can generally find a way to do things without them.
Anything other than e-mails/notifications being sent at certain times can generally be handled without cron jobs. Mind if I ask what's supposed to happen when endDateTime is reached?
Again, I'm not telling you to stay away from cron jobs, just that there's usually more than one way to do something.
eventide
08-23-2011, 09:30 AM
Thanks a lot for replying, aj
The proposed site is something of an auction site. Products will be bid upon by registered users, and there will be an end time to each product's bidding period, at which time the winning bidder will receive an email and, obviously, relevant db records will be updated to close the bidding on the product.
If there are alternatives to cron jobs for automatically running scripts, what are they?
Thanks again!
aj_nsc
08-23-2011, 10:14 AM
Nope, no alternatives for automatically running scripts - it's just that often enough people look to cron jobs when actually, then don't need something to be done at a specific time, they just need something to be done BEFORE the next time some sort of access is requested - at which point you can just get your accessing script to do the cron jobs work for you.
In your case, however, you cannot rely on that scenario happening. Unfortunately, cron jobs don't help you out 100% either, because you can't (unless you want to be quite complex about it) get a cron job to run whenever you want it to (i.e. at the end of an auction). A cron job runs when it is scheduled - e.g. 12:00 every day, every 2 hours on weekdays, at 1:24pm on the second monday of every third month starting in January, etc, etc, etc.
Best thing to do is a combination of what I've suggested above. Every time somebody accesses your site, I'd get a script to run in the background to check what auctions have ended and update/email whatever records need to be updated.
Also, set up a cron job that does basically the exact same thing and set it to run at a certain time once a day every day.
That way, your auction winners will generally be notified very shortly after the auctions have been closed, if your site is reasonably high traffic, otherwise, they'll all be notified at a certain time of the day - kind of like a failsafe.
If you haven't already, check out:
http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/CronJobs
eventide
08-23-2011, 10:20 AM
Thanks for the great advice, aj!
I'll look into all of that.
Regards,
Mark
svidgen
08-23-2011, 11:53 AM
You can have a cron job run once per minute. As long as your tasks (scheduled auctions) don't need to run at a particular second in the minute. Though, if that's needed, you can always have the task collect all events for the coming minute and fire them as necessary.
The cron job looks like this:
MAILTO=youremailaddress@yourdomain.com
* * * * * /home/your_username/run_every_minute.php
And your run_every_minute.php (or other script type) is whatever you need it to be.
aj_nsc
08-23-2011, 12:11 PM
Is running a cron job once every minute is a bit of unnecessary overhead? (Maybe not, this is more of an "whats your opinion experts because I don't really know" type question than me asserting an opinion over something I don't really know that well).
svidgen
08-23-2011, 12:39 PM
Yes and no.
Starting a new process once per minute on a most servers (or even most laptops) is super-trivial. So, the deciding factor in my opinion is the amount of work the process does. And that work may be drastically reduced by running the job more often. If you run it once per hour, for instance, it only affects performance once an hour, but it hits you with a full hour's worth of catch-up work. And that could get very noticeable at 4, 5, and 6pm.
Setting up a process to run once-per-minute is a the safest and easiest solution for new developments.
That said, there's a point at which a minutely cron job running on (one of) your web server(s) simply isn't the best solution. At the point where your site becomes inundated with records that need to be processed once-per minute, you'll eventually need a solution that processes the queue continuously in a more fine-tuned and rate-limited manner. But, at that point you have enough traffic coming through your site to justify rewriting run_every_minute.php's functionality in your brand new process_queue_daemon.c.
eventide
08-23-2011, 02:23 PM
Great info, svidgen!