views:

2034

answers:

6

Hi,

I am using MYSQL as my database and PHP as my programming language.I wanted to run a cron job which would run until the current system date matches the "deadline(date)" column in my database table called "PROJECT".Once the dates are same an update query has to run which would change the status(field of project table) from "open" to "close".

I am not really sure if cron jobs are the best way or I could use triggers or may be something else.Also I am using Apache as my web server and my OS is windows vista.

Also which is the best way to do it? PHP scheduler or cron jobs or any other method? can anybody enlighten me?

A: 

I'd see http://stackoverflow.com/questions/1267918/how-can-i-set-up-cron-job-in-my-site-using-php/1267958#1267958

arbales
-1, not Windows related
Alix Axel
+1  A: 

I would always go for a cron job for anything scheduling related.

The big bonus point is that you can echo info out as well and it get's emailed to you.

You'll find once you start using cronjobs, it's hard to stop.

Mark L
but hows it done on a web site? like commercial web sites do it with the help of cron jobs? is cron jobs the best way there too? because we can change hosts and may need to set it up everytime we change hosts.?
You'd need to do it from a shell account on the host itself. If you SSH into your web server, pinpoint the php file you want to run, type crontab -ethen add in the command, i.e.*/5 * * * * /usr/bin/php /home/youraccount/website/file.phpThat should run it every five minutes or so. Search google for crontab generators and they'll help you fine tune the timings and generate the command strings for you.You would need to save the crontab when transfering between hosts:crontab -l > myct.txtand on the new server, I think it's crontab < myct.txt (or just copy and paste manually)
Mark L
A: 

cron does not exist, per se, in vista, but what does exist is the standard windows scheduling manager which you can run with a command line like "php -q -f myfile.php" which will execute the php file at the given time.

you can also use a port of the cron program, there are many out there.

if it is not critical to the second, any windows scheduling application will do, just be sure to have you PHP bin path in your PATH variable for simplicity.

A: 

how about PHPscheduler..R they not better than cronjobs? I think crons would be independent of the application hence would be difficult if one has to change the host..i am not really sure though..It would be great if anyone can comment on this!! Thanks!

+1  A: 

I think your concept needs to change.

PHP cannot schedule a job, neither can MySQL. Triggers in MySQL execute when a mysql query occurs, not at a specific time. Neither

This limitation usually isn't a problem in web development. The reason is because your PHP application should control all data going in and out. Usually, this means just the HTML that displays that data, or other formats to users, or other programs.

In your case you can think about it this way. The deadline is a set date. You can treat it as data, and save it to your database. When the deadline occurs is not important, it is that the data you have sent in your database is viewed correctly.

When a request is made to your application, check if the date of the deadline is in the past, if it is, then display that the project is closed - or update that the project is closed, just before display.

There really is no reason to update data independantly of your PHP application.

Usually, the only things you want to schedule are jobs that would affect your application in terms of load, or that need to be done only once, or where concurrency or time is an issue.

In your case none of those apply.

PS: I haven't tried PHPscheduler but I can guess it isn't a true scheduler. Cron is a deamon that sleeps until a given task is due in its queue, executes the task, then sleeps till the next one is due (at least thats what it does in the current algorithm). PHP cannot do that without the sockets and fork extensions, as special setup. So PHPscheduler is most likely just checking if a date for a task has expired, on each load of a webpage (whenever PHP executes a page). This is no different then you just checking if the date on the project has expired, without the overhead of PHPScheduler.

bucabay
+1  A: 

For Windows CRON jobs I cannot recommend PyCron enough.

Alix Axel
+1: I've had good success with it too.
Jim Ferrans