Spudinski Posted February 11, 2012 Posted February 11, 2012 (edited) I thought I'd share this with you lot. It's basically a drop-in function for any interval based tasks. It was designed to be used in an OOP application, but I've [slightly] rewritten this to be used within a procedural environment. The function has PHPDoc, so I won't be dissecting the internals of it step by step, just read it. It uses JSON for the crontab file, as it's one of my New Year's goals to get away from XML. Though, another reason I made this was to perform automated tasks with inner classes and functions. So, with security in mind, I'd suggest transforming the "call" to a function somewhere, and add call_user_func(). See below for the script, and an example JSON crontab file. Feel free to comment, but do not spam this thread with "help me" messages. PHP: /** * crontab() * <p> * A <i>very</i> simple replacement for the crontab found on *nix systems. * <br /> * It has the ability to run a single cron multiple times, so it should * handle all the crons within the crontab effectively. * </p> * @param string $crontab_file An absolute path to the crontab file. * @return boolean Returns true if any crons were executed, false if not. * @example * <code> * <?php * * crontab(); * // your usual scripting * * ?> * </code> * @author Spudinski<br /> * You can contact me at me[]spudinski.tk with any queries or suggestions. * @license http://creativecommons.org/licenses/by-sa/3.0/<br /> * Just, say thanks. */ function crontab($crontab_file) { $cron_file = file_get_contents($crontab_file); $crontab = json_decode($cron_file, true); $crontab_restruct = array(); $crons_ran = 0; foreach ($crontab as $cron_key => $cron) { $cron_runtime = (time() - $cron['last_run']); if ($cron_runtime > $cron['interval']) { $run_times = $cron_runtime / $cron['interval']; if (!file_exists($cron['call'])) return false; for ($i = 1; $i <= floor($run_times); $i++) { require($cron['call']); $cron['last_run'] = time(); $crons_ran++; } } else continue; $crontab_restruct[$cron_key] = $cron; } if ($crons_ran > 0) { $json_restruct = json_encode($crontab_restruct); file_put_contents($crontab_file, $json_restruct); } return ($crons_ran > 0) ? true : false; } JSON example: {"update_stats":{"last_run":1329002545,"interval":60,"call":"/path/to/update_stats.php"}} Ps. "interval" is in seconds, not minutes. That's it for now, 'cause I'm lè tired. Edited February 12, 2012 by Spudinski File existence check, method of inclusion. Quote
Anonymous Posted February 12, 2012 Posted February 12, 2012 How, for the love of god is this even remotely useful? Sheesh JSON? I mean seriously? for cron jobs? What fracking planet did you think this would be useful on? Then there is a laughabale loop that .. wait for it ... requires_once the job.. which will only execute once.. <?php /* y.php */ require_once('x.php'); require_once('x.php'); <?php /* x.php */ echo "me!\n"; $ php -f y.php me! Do you not actually do testing Spud? Try reading the manual next time, or at least paying attention. Quote
Sim Posted February 12, 2012 Posted February 12, 2012 roflmao @ANoymous post about "what planet" sorry.. uncalled for posting this myself. Quote
KashBFD Posted February 12, 2012 Posted February 12, 2012 Hmmm and i was trying to make sense of it being a ameature.. still deciding wether to do so.. :/ Quote
Spudinski Posted February 12, 2012 Author Posted February 12, 2012 How, for the love of god is this even remotely useful? Sheesh JSON? I mean seriously? for cron jobs? What fracking planet did you think this would be useful on? Then there is a laughabale loop that .. wait for it ... requires_once the job.. which will only execute once.. <?php /* y.php */ require_once('x.php'); require_once('x.php'); <?php /* x.php */ echo "me!\n"; $ php -f y.php me! Do you not actually do testing Spud? Try reading the manual next time, or at least paying attention. Oh, for the love of... Actually, you know what? I've got better things to waste my time on. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.