php script delete error_log file

At times, I get inquiries for deleting error_log files from the server as they are consuming space. I strongly recommend not to delete the files. Those files contains critical information those cannot be seen through a normal debugging process. Trust me, error_log files are life saver. But, still I would like to publish a small PHP script here in order to delete the error_log files from the server.

Here is the script.

#!/usr/bin/php
getFilename(), $file2search) !== FALSE ) {
@unlink(
$fileinfo->getPath().
DIRECTORY_SEPARATOR.
$fileinfo->getFilename()
);
}
}
?>
The above script will find all files starting with “error_log” (e.g. error_log, error_log.txt, error_log.txt.gz etc) and delete those from their respective locations. This script can be set as a cron job in a server where it should run once a week (may be Sunday Midnight when the server experiences lesser hit).

I hope it helps somebody and I am open for any comments.

Script to delete error_log files..
Hi!

I have two Linux dedicated servers in HostGator.
I need someone to makes me a WHM or Cpanel scripts that once a week should search and delete ALL error_log files.
Is that possible?

The script is simple:

find /home/ -type f -name error_log -delete

Just put in root’ crontab.

What I need is a script that finds and searches ALL “error_log” files hosted in the websites inside 2 dedicated servers in HostGator. I have WHM installed, and I also have SSH Access.

They are just “error_log” files that the server generates when there is a 404, 505, etc. error. They are generated inside the directory where the error occurrs. And in the WHM panel of the dedicated server there are no options to “SET OFF” this, or to limit the sizes of the “error_log” files..

The most correct solution is
1. Detect what lines are logged into error_log’s and fix the problem
2. Disable logging to error_log into /home/

To eliminate all the error log is going to cause clients issues. Some may need to see some of it.

Easiest solution would be to tell php NOT to log errors, set ERror reporting to none:
error_reporting = ~E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = Off

error_log is generated by php, so disabling would eliminate this problem all together.

If you’d still like to provide the error_log, but only in small doses, use logrotate script, or write a bash command that looks for error_logs over a certain size, copies the last 10 lines(just an example) to a temp file, removes the old error_log, puts the temp in it’s place, chown it, and you’re done there.

I have a few cpanel servers for shared hosting. I have noticed that there are many error_log files in the public_html directories of many websites and also inside sub-directories of many websites. I want to delete all those individual error_log files. Can someone give me a command to find and delete these error log files?

Advertisements