Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm new at hosting my VPS on a CentOS linux server.

The following log files grow quite rapidly and I would like to have them deleted every 24 hours:

/home/httpd/tanguay.info/stats/tanguay.info-custom_log
/home/httpd/tanguay.info/stats/tanguay.info-error_log

/home/admin/__processed_stats/*

What would be the steps for me to create a cron job that deletes all of these files every night at midnight?

share|improve this question
    
This question has been answered thouroughly by the community. Please accept an answer. –  Jan Jungnickel Jul 24 '09 at 15:43

4 Answers 4

up vote 5 down vote accepted

It is not recommended to immediately purge logfiles as they may be needed later for debugging, statistics or tracing an intrusion. While log shipping offsite may be overkill in a single VPS scenario, there are better approaches than simply deleting those.

I suggest you look into logrotate or cronolog (Probably available as package on CentOS). With these packages you can set up rules how to process logfiles on a (usually) daily basis, like: Keep a week worth of logs, but compress all of them except today's.

share|improve this answer
    
I'm pretty sure that logrotate will work out-of-the-box for httpd as soon as you install the relevant package. I mostly stick with the default of rotate weekly, keep 4 weeks of logs - the possibility to look at month-old logs has been useful to me more than once. –  Marie Fischer Jul 21 '09 at 0:40

You can also reduce the information being sent to the logs. You can set it to only log errors instead of information, warnings, and errors. Just a suggestion. If you want to delete the logs, make sure they are still not in use. Rotate your log files like everyone else has suggested. They usually get compressed and deleted after a period of time.

share|improve this answer

Logrotate is the best method. In any case, you need to send the HUP signal to httpd afterwards, otherwise the filehandle for your log gets left open and your filesystem will fill up but won't be reflected in the sizes of the files.

share|improve this answer
    
+1 for the comment about open file handles. You could however use cronolog for apache logging, and then delete the previous days log as generated by cronolog without having to worry about file handles. Either way using cronolog over logrotate means that apache isn't restarted for log rotation, which is a good thing imo. –  theotherreceive Jul 21 '09 at 2:43

To empty the files every day at 1AM you may use :

0 1 * * * :> /home/httpd/tanguay.info/stats/tanguay.info-custom_log
0 1 * * * :> /home/httpd/tanguay.info/stats/tanguay.info-error_log
0 1 * * * for i in `ls /home/admin/__processed_stats/`; do :> $i; done

But the good way is to use logrotate

You may also want to take a look to : http://serverfault.com/questions/42527/log-rotation-tool-that-keeps-only-a-specified-amount-of-logs-and-discards-everyth and http://serverfault.com/questions/34387/apache-access-log-rotation

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.