LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   need script to empty logs (https://www.linuxquestions.org/questions/linux-general-1/need-script-to-empty-logs-3060/)

drobson 06-04-2001 04:40 PM

I want a script that will allow me to either completely
empty the contents of
various log files or perhaps save only the previous days entries and erase all older entries.
I thought I remember reading having to do something about the various demons that wrote to those log files before you
could operate on them,
but I can't remember where I might have read it, nor can I remember the details.
I know experienced sysadmins do this kind of thing all the time in cron, but I'm a newbie just learning, so help would
be much appreciated.

Doug Robson
drobson@chartertn.net

aimstr8 06-04-2001 07:44 PM

Doug,

First read the man page on logrotate.

I am not much of a programer, but the below script is a hack at doing what it sounds like you want. Just change it to suit your needs and put it in a cront job.


--------------------------------START HERE-----------------------------------
!/bin/ksh
#
#set defaults
# Set date & time stamp

date=`date|cut -c5-11,25-|sed 's/\([0-9]\{1,2\}\)/\1,/'`
time=`date +"%D"|sed 's/0//'`
time2=`date +%d/%b/%Y`
#
cat /var/log/messages|grep INVADING|grep "$time"> /var/log/test.log
cat /var/log/httpd/access_log|grep "$time2">> /var/log/test.log
#
#
# Control File Size
#
big=`ls -l /var/log/test.log |awk '{print $5}'`

if [ "$big" -gt 600 ]
then
echo "test log is $big in size as of `date +"%D %T"`" | mail -s "Test HTML log File `date +"%D %T"`" root

tar cfz /var/log/test.log.tar.gz /var/log/test.log
sleep 2
mv /var/log/test.log.tar.gz /var/log/test.log.tar.gz.`date +%Y.%m.%d`
sleep 2
#mv /var/log/test.log.tar.gz* /var/log/OLD/
ls -lh /var/log/test* | mail -s "Test HTML log File `date +"%D %T"`" root
#ls -lh /var/log/OLD/test*| mail -s "Test HTML log File `date +"%D %T"`" root

else
echo "Test HTML log file is good at $big"| mail -s "Test HTML log File is good at $big on `date +"%D %T"`" root
fi


All times are GMT -5. The time now is 10:53 AM.