FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


script show off- simplecron(tmp name, might change)





Mgccl
The main cron script, going to be called by a PHP genorated image
simplecron.php
Code:
<?php
function func_simp_cron(){
   $timenow = time();
   $filename = 'cronjob.inc';

   $fp = fopen($filename, 'r');
   $data = fread($fp,filesize($filename));
   fclose($fp);
   $data = explode("\n|JOB|",$data);
   $i = 0;
   while($i < (count($data)-1)){
      $n = $i+1;
      $data[$n] = explode('|',$data[$n]);
      $cron[$i]['last_run'] = $data[$n][0];
      $cron[$i]['interval'] = $data[$n][1];
      $cron[$i]['file'] = $data[$n][2];
      $i++;
   }

   $i = 0;
   while($i < count($cron)){
      if ($timenow > ($cron[$i]['last_run']+$cron[$i]['interval'])){
         $return[] = $cron[$i]['file'];
         $string .= "\n|JOB|".$timenow;
      } else {
         $string .= "\n|JOB|".$cron[$i]['last_run'];
      }
      $string .= '|'.$cron[$i]['interval'].'|'.$cron[$i]['file'];
   $i++;
   }

   $fp = fopen($filename, 'w+');
   flock($fp, LOCK_EX);
   fwrite($fp, $string);
   flock($fp, LOCK_UN);
   fclose($fp);
   return $return;
}

$simp_cron_job = func_simp_cron();
if (is_array($simp_cron_job)){
foreach ($simp_cron_job as $simp_cron_run){
   include ($simp_cron_run);
}
}
?>


PHP genorated image + call simplecron....
Code:

<?
// use <img src="cron_img.php" width="1" height="1" alt="" />

Header('Content-Type: image/gif');
include('simplecron.php');
echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
?>


Code:


|JOB|1159224254|2000|a.php
|JOB|1159224254|3000|a.php

the script define what to run in cron... important... there is an \n before the first |JOB|

this should be one of the fastest PHP cron script you could find! any suggestsion?
hexkid
Mgccl wrote:
any suggestsion?

What's wrong with a real cron job?

If your server gets disconnected for sometime, the "cron job" will not run.
People who browse with images disabled (or just 1x1 images) will not fire your "cron job".
The scripts specified in cronjob.inc are directly callable from the web browser? Can a user mess up your "cron job" schedule?
The scripts called from your "cron job" cannot do any output, or the image will be garbled ...


As for the code maybe you could look into file(), instead of fopen(), fread(), explode().
Mgccl
ummm... normally... cron job does not output things...
why I want a fake cron... than real one is because...
if a server with no cron.. still works
Marston
Mgccl wrote:
ummm... normally... cron job does not output things...
Incorrect, but why reinvent the wheel anyways?
Mgccl
because... I suggest there might be a place where server are not linux
hexkid
Mgccl wrote:
because... I suggest there might be a place where server are not linux


You have Scheduled Tasks in Windows Smile
Esch
If you really do want to run your task via PHP on a webserver, you can use the functions ignore_user_abort() and set_time_limit(0) to make your script run forever, regardless of the user's connectivity.

It'd probably be a good idea to use a sleep() in whatever loop you have running in order not to overload your webserver. Also, a "lock" file containing a flag that states that the process is running would be a good idea, so you don't start two accidentally -- you could even use a lock file to signal to the script when to end (which the script would have to check periodically, of course).

In short, it's probably ok to do such a thing if you really need it to run purely on your webserver, but, as everyone else said, cron jobs/Scheduled Tasks are much more elegant solutions.
Related topics
Show off corner
751 Useful Windows XP Files
Looking for easy FormMail Script
Can any girl help me out?
The 5 Golden Rules of Professional Design
CHUCK NORRIS
Bad news about the Server 2
A small PHP script request(like SO small), High pay
Show Off Your Back Yard
show off your site
Free Script To Show Off Websites ?
Name server change request
Legend of Kage
Possible War with Korea
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.