“Progress bar” for long running cron scripts in PHP

“Progress bar” for long running cron scripts in PHP

Long running scripts can be a pain if you don’t know how much will be running. If you need to see a progress and flush() or ob_flush() do not work because of browser cache you can implement an “email progress bar”.

PHP example:

$step = 0;
$last_percent = 0;
$total_rows =
while(1) {
   $step++; //count step

   // here should be is the code witch consume time for every loop
   // end consuming time code

   $percent= ($step * 100) / $total_rows;
   if ($last_percent != round($percent)){
      // because you do not want to receive an email for every step
      $last_percent = round($percent);
      if (($last_percent % 10) == 0){
         // this will send email from 10 % to 10 % - total 10 emails
         mail(your_email_here, 'Progress '.$last_percent.'% done' , "Progress: ".$contor."/".$total_rows);

} // end while

As you can see the idea is very simple and you can implement it in any language.

No comments.