How to rotate log files

We can use simple linux commands to rotate logs. There are some tools also available to rotate the logs.
Method 1: Backup the log and make the log empty without stopping the service.
1.1 Backup the current Log

$ tail -100000 alert_UPGR.log > alert_UPGR.log.bak

$ ls -ltr alert_UPGR.log*
-rw-r—– 1 oracle oinstall 353665 Jul 4 00:41 alert_UPGR.log
-rw-r–r– 1 oracle oinstall 353665 Jul 4 00:52 alert_UPGR.log.bak
1.2 Compress the backed up Log

$ gzip alert_UPGR.log.bak
$ ls -ltr alert_UPGR.log*
-rw-r—– 1 oracle oinstall 353665 Jul 4 00:41 alert_UPGR.log
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
1.3 Empty the active log

$ >alert_UPGR.log

$ ls -ltr alert_UPGR.log*
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
-rw-r—– 1 oracle oinstall 0 Jul 4 00:53 alert_UPGR.log
1.4 Confirm the alert log is updated

SYS@UPGR:SQL> alter system switch logfile;

System altered.

SYS@UPGR:SQL> exit

$ ls -ltr alert_UPGR.log*
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
-rw-r—– 1 oracle oinstall 229 Jul 4 00:54 alert_UPGR.log
2. How to rotate Catalina.out log when it is more than 5M?.

2.1. Create this file

/etc/logrotate.d/tomcat

2.2. Copy the following contents into the above file

/var/log/tomcat/catalina.out { copytruncate daily rotate 7 compress missingok size 5M }

About the above configuration:
Make sure that the path /var/log/tomcat/catalina.out above is adjusted to point to your tomcat’s catalina.out
daily – rotates the catalina.out daily
rotate – keeps at most 7 log files
compress – compressesthe rotated files
size – rotates if the size of catalina.out is bigger than 5M