tags:

views:

122

answers:

4

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?

Edit:-------------------- Preferably without using Cron Jobs.

+6  A: 

If you are running on linux you would use a cronjob

On most distros there is a command called crontab that schedules tasks for you and a specific format you need:

0 0 * * * php /path/to/file.php

EDIT: Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?

You may not even need to specify the php part if the php file is marked as executable i.e

0 0 * * * /path/to/file.php

Just do "chmod +x /path/to/file.php" form the linux command line

xenon
+1 for suggesting cronjob
Nadia Alramli
just curious if there is another way to do it. If in the future cron jobs are not available. I have tried to set up a cron job but it seems it cannot find the file. /usr/local/bin/php -q /htdocs/www/mysubdirectory/cron.php I am doing this as seen on http://www.modwest.com/help/kb5-125.html
Chris B.
sorry:/usr/local/bin/php -q /htdocs/www/mysubdirectory/cron.php Is the cron job I am using
Chris B.
have you done a "which" to see if this is true location of PHP on that machine?
xenon
what is a "which"?
Chris B.
sorry, "which" is a command on linux/unix that shows the full path of shell commands. So from the command line if you do "which php" it should return the path where php is
xenon
Jet
+1 for the cron job. or, you can do it by yourself every midnight :)
markuz
A: 

You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).

You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.

acrosman
This is what I am trying to avoid in the first place. Thank you though.
Chris B.
Then I think you're back to cron.
acrosman
A: 

Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.

If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

ceejayoz
+1  A: 

There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)

Here's a google search: **oops I'm new so I can't post URL apparently. Just search for "web based cron".

Good luck /0

0scar
"Remember that PHP-scripts timeout after like 30secs.)" You can counter this with set_time_limit(0);
xenon