This script creates a copy of standard out log files of Weblogic Server after it has reached a predefined size limit,renames it with current date & generates a fresh log file of zero byte size.
#!/usr/bin/perl # Name: rotateLog.pl # Script to rotate any log file. # Usage: rotateLog.pl my.log # use strict; my ( $LOGFILE, $MAXLOGS, $DELNUM ); $LOGFILE = $ARGV[0]; $MAXLOGS = 30; print $LOGFILE; # if ( -e $LOGFILE ) { my ( @LOGS, $LOG, $LASTNUM ); @LOGS = `ls -1 $LOGFILE?*`; $DELNUM = ( scalar(@LOGS) - $MAXLOGS ); if ( $DELNUM gt 0 ) { print "Need to delete $DELNUM files.\n"; } foreach $LOG( @LOGS ) { chomp($LOG); # Delete logs until less than MAXLOGS if ($DELNUM gt 0) { print "Deleting log $LOG.\n"; unlink $LOG; $DELNUM = $DELNUM -1; } # Strip log number from log $LASTNUM = $LOG; $LASTNUM =~ s/$LOGFILE//g; } # Increment log number for new file $LASTNUM ++; $LASTNUM = sprintf("%03d", $LASTNUM); # Copy current log to backup print "Rotating $LOGFILE to $LOGFILE$LASTNUM.\n"; system("/usr/bin/cp $LOGFILE $LOGFILE$LASTNUM"); # Create replacement current log system("/usr/bin/cat /dev/null > $LOGFILE"); # } # else { # print "Error: Log file $LOGFILE not found.\n"; # }