I am developing a web app which requires events. The user creates an event at a specific time, and I need a script to be executed and activate the event at the exact time, so the event can start. Any ideas on how I can do this? I googled and searched through stack overflow, but all I encountered was multiple execution at specific time. What I need is execution at automatically set time. Hope you help me, I'm desperate.
-
2In all your googling, you never encountered `cron`? – Mark Baker Apr 02 '13 at 08:53
-
1Cron Jobs. Goooooogle them :) – Hanky Panky Apr 02 '13 at 08:53
-
I did, but as I commented below I am not sure if I can install cron jobs – Mark Apr 02 '13 at 09:09
-
how about finding out first, before looking for alternatives – Apr 02 '13 at 09:14
-
ok, I'll try it :) can you please post a download link for cron jobs? – Mark Apr 02 '13 at 09:17
-
This is actually not a duplicate, as the answer marked as duplicate is specific to (a) Windows and (b) a slow job and (c) a command sysadmin job as opposed to a general purpose job. – cmc Apr 03 '13 at 08:28
2 Answers
You have 3 options. My recommendation: Use cron if you can, user driven if you have to, and daemon as a last resort.
(1) cron (as mentioned in comments)
cron is a scheduler for linux systems that will run a command line job on your system. You log into your server over ssh, type crontab -e
, and add a line like this:
4 5 * * * php /path/to/my/script.php
This would run the script at 5:04 a.m. every day.
<?php
// /path/to/my/script.php
// Do something
Some hosting services allow entering cron jobs with a GUI. There are also external cron services, that will call a URL for you at specific times.
(2) daemon
This is the most advanced option and also the least reliable: You run a command line script that contains an infinite loop. The script then periodically checks state and responds to it. Because it is likely to crash after months of running, you have to have a second script to restart it in case it does. This is a lot of work, but it is the most flexible approach.
<?php
while (1) {
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
sleep(5);
}
3. user driven
If you have a decent amount of traffic on your site, you can simply include a the job this in your page footer, when there is no more output. Make sure this code is fast, or an unlucky user will be waiting for it.
<?php
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
There are also to more fancy approaches of "user driven" that I haven't personally tested in another stack overflow question.
-
-
1One further option is MySQL EVENTS - http://dev.mysql.com/doc/refman/5.6/en/create-event.html – Mark Baker Apr 02 '13 at 10:02
-
thank you very much! I think I will use the user driven method, it's not something very important that must be server-side solved after all :) – Mark Apr 02 '13 at 22:53
-
let's try user driven, I just need to send a "1" or a "0" at specific times. – Roberto Sepúlveda Bravo Nov 03 '16 at 18:35
What u are looking for is CRON JOBS
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

- 68,075
- 43
- 96
- 126
-
I am not sure if I can install cron jobs on the server, is there an alternative? – Mark Apr 02 '13 at 09:08