Deleting error_log automatically | andi setiawan's blog

I’ve been noticed by my hosting disk space warning, the indicator bar was red. Why? Oh, the disk space was decreased automatically because the error_log is growing up every my scripts executed. So, I have to delete it every time it’s growing up.

Why?

Why error_log appear automatically in my hosting directories? it is because the hosting guy has turned on the error logging, so the error on the php script won’t be displayed when it’s executed, but store the error string into a file named “error_log”.

Why there’s so many of them? Because most of the directories on my hosting has a PHP script inside it.

Why the size of them are so big for a file? Well, frankly, there are some error inside the PHP engine used by my hosting server and it is not fixed yet until now. Unfortunately, every time a script executed, the size of error_log will grow up. Even when you deleted the file, the file will remain growing up every your site loaded. So, it is important to inspect your directories, and delete error_log frequently.

How?

The first thing I’ve done is by deleting it manually, but it will waste your time. So I created a script to do that automatically, the script also collect the size of total error_log detected.

When?

I set the script to run once every day, you can do that by utilizing “cron job” in the cPanel (see documentation)

Here’s a sample of simple PHP cron Job:

/usr/local/bin/php -f /home/(username)/public_html/(scriptname).php

Who?

Of course I am the person who code this script, but you can do that too by using my script,.

Just copy my script in your root directory, then run it on your browser, or run it automatically thru Cron Job.

Demo

See the demo here: http://otakbali.com/hapuserror.php 

The Script

Copy and paste the script bellow to a PHP script. Or download it here.

<?php
// set the time limit first, remember, if you have 
// a huge number of files, it will be useful
set_time_limit(36000);
$dir = "./";
$num=0;
$bytes=0;
$deleted=0;
$bytesdel=0;
$echo="";
 
echo "Looking for <b>error_log</b> in every directories <br/>";
 
buka($dir);
 
$bytes=format_bytes($bytes);
$bytesdel=format_bytes($bytesdel);
echo "Found $num error_log files ($bytes), 
and deleted $deleted ($bytesdel) files."
;
echo "<hr/> <ol>$echo</ol>";
 
 
// below are the functions, modify it on your risk
 
 
// open directory, search error_log, delete it
function buka($dir){
  global $num, $deleted, $bytes, $bytesdel, $echo;
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           if(($file!=".") and ($file!="..")){
              
              // cek sub dir,
              if($dir=="./"){
                $cek=$file;
              } else {                
                $cek="$dir/$file";
              }
              $type=filetype($cek);
              if($type=="dir"){
                
                // kalo tipenya dir, buka..
                buka($cek);
                
                } else {
              
                // cek file error_log
                if($file=="error_log"){
                  // jumlah yg ketemu
                  $num=$num+1;
                  $ukuran=filesize($dir."/".$file);
                  $bytes=$bytes+$ukuran;
                  $ukurannya=format_bytes($ukuran);
                  if($dir=="./"){
                    $filenya=$file;
                  } else {                
                    $filenya="$dir/$file";
                  }
                  
                  $echo.="<li>$filenya ($ukurannya ) </li>";
                  //kalo ketemu, delete
                  $del=unlink($filenya);
                  //kalo deleted, naikkan value $num
                  if($del){
                    $deleted=$deleted+1;
                    $bytesdel=$bytesdel+$ukuran;
                   }
                }
              }              
           }
       }
       closedir($dh);
   }
}
 
}// end function
 
function format_bytes($bytes) {
   if ($bytes < 1024) return $bytes.' B';
   elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
   elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
   elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
   else return round($bytes / 1099511627776, 2).' TB';
}
?>

Posted in php, programming, Tips n Trik | Tagged , |

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: