Operating System - HP-UX
1753579 Members
6237 Online
108796 Solutions
New Discussion

Re: Log Rotation Script for Tomcat Logs

 
Ajin_1
Valued Contributor

Log Rotation Script for Tomcat Logs

Hi Experts

 

we are using Apache Tomcat and the Log files are generated in one Log DIR in the below said format.

 

$ cd SSOlogs
$ ll
total 48
-rw-r--r--   1 dz900439   unyo          1854 Jan 18 20:15 filecreate.sh
drwxr-xr-x   2 dz900439   unyo          8192 Jan 21 16:25 madhutest
-rw-r--r--   1 dz900439   unyo           307 Jan 21 16:36 testmove.sh
-rw-r--r--   1 dz900439   unyo             0 Dec  1 10:10 tomcat.log.Dec1
-rw-r--r--   1 dz900439   unyo             0 Dec 10 10:10 tomcat.log.Dec10
-rw-r--r--   1 dz900439   unyo             0 Dec 11 10:10 tomcat.log.Dec11
-rw-r--r--   1 dz900439   unyo             0 Dec 12 10:10 tomcat.log.Dec12
-rw-r--r--   1 dz900439   unyo             0 Dec 13 10:10 tomcat.log.Dec13
-rw-r--r--   1 dz900439   unyo             0 Dec 14 10:10 tomcat.log.Dec14
-rw-r--r--   1 dz900439   unyo             0 Dec 15 10:10 tomcat.log.Dec15
-rw-r--r--   1 dz900439   unyo             0 Dec 16 10:10 tomcat.log.Dec16
-rw-r--r--   1 dz900439   unyo             0 Dec 17 10:10 tomcat.log.Dec17
-rw-r--r--   1 dz900439   unyo             0 Dec 18 10:10 tomcat.log.Dec18
-rw-r--r--   1 dz900439   unyo             0 Dec 19 10:10 tomcat.log.Dec19
-rw-r--r--   1 dz900439   unyo             0 Dec  2 10:10 tomcat.log.Dec2

 

 

Maximum number of days for logs to be deleted in one Month

 

 

find /home/dz900439/SSOlogs -type f -name "tomcat.*" -mtime +30 -exec cp /tmp/SSOlogs {} \;
echo "waiting for 30 secs"
sleep 10
#find /tmp/SSOlogs -type f -name "tomcat.*" -mtime +30 -exec /usr/contrib/bin/gzip {} \;
find /home/dz900439/SSOlogs -type f -name "tomcat.*" -mtime +30 -exec ls -l {} \; | wc -l
exit

 

 

I used the sample above said script

 

My reqirement was i want to keep 10 days log in the orginal location and move the old logs day by day or once in a week to some other location /tmp and zip the old logs in the /tmp and delete the log file.

 

 

Thanks in Advance.

 

 

Thanks & Regards
Ajin.S
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Log Rotation Script for Tomcat Logs

>find /home/dz900439/SSOlogs -type f -name "tomcat.*" -mtime +30 -exec cp /tmp/SSOlogs {} \;

 

You need to swap the order: ... -exec cp {} /tmp/SSOlogs  \;

 

>My requirement was I want to keep 10 days log in the original location and ...

 

Older than 10 days?  Something like this:

 

TMP=/tmp/SSOlogs
LOGS=/home/dz900439/SSOlogs

mkdir -p $TMP

find $LOGS -type f -name "tomcat.*" -mtime +10 |  while read file; do
      base=${file##*/}
      /usr/contrib/bin/gzip -c $file > $TMP/$base.gz
      if [ $? -eq 0 ]; then
         # Keep same date
         touch -r $file $TMP/$base.gz
         rm -f $file
      fi
done

Ajin_1
Valued Contributor

Re: Log Rotation Script for Tomcat Logs

Hi Dennis

 

Thanks for your reply .I checked the same script with test environment  .Its moving files from orginal location to /tmp and zip the files in /tmp location . I plan to put it into cron for sheduling ,how can i set how frequence in crontab?

 

0  0  *  *  *  /home/dz991173/logrotate.sh

Thanks & Regards
Ajin.S
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Dennis Handly
Acclaimed Contributor

Re: Log Rotation Script for Tomcat Logs

>how can I set how frequently in crontab?

>00  00  *  *  *  /home/dz991173/logrotate.sh

 

(I recommend always using two digits so it lines up.)

 

This does it everyday at 0000, midnight:

MM HH DD mm DoW command

 

If you want it every 12 hours, use a comma to repeat:

00 00,12 * * * command ...