Logfile rotation script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logfile rotation script.
# 1  
Old 08-24-2009
Logfile rotation script.

I'm trying to find or create a Perl script that:
Checks for and creates these files:

notes
notes.1
notes.2
notes.3
notes.4

The first represents the current log file and the others are older versions. Each time the script runs it would check for the existence of notes.3 and, if it exists, move it to notes.4. Then is should check for notes.2 and move it to notes.3 and so on, until notes is moved to notes.1.

I've looked on the net already, but they're just too complex and try to do too much compared to my requirements.

Does anyone know of a script that does this, or do you have any code that could be adapted easily?

Thanks!
# 2  
Old 08-24-2009
With awk:

Code:
ls notes* | 
awk -F"." 'NF>1{d=$2+1; system("echo "$0 " " $1 "."  d)}END{system("echo notes notes.1")}'

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 08-24-2009
Cool thxz for that!

I was just fumbling around and am testing this code out now and seems to be working for what I need. Thxz anyways for your help will save it; since other ways are always helpful too, thxz.

Code:
#!/usr/bin/perl 
 
use strict; 
use warnings; 
 
use File::Copy; 
for my $i ( 3, 2, 1 ){ 
  if( -e "notes.$i" ){ 
    my $j = $i + 1; 
    move( "notes.$i", "notes.$j" ) or die "Could not move notes.$i to notes.$j: $!\n"; 
  } 
} 
if( -e "notes" ){ 
  move( "notes", "notes.1" ) or die "Could not move notes to notes.1 $!\n"; 
}

# 4  
Old 08-24-2009
BTW, I forgot to mention that the echo command must be replaced with the mv command if the command works as expected.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logfile monitoring with logfile replacement

Bonjour, I've wrote a script to monitor a logfile in realtime. It is working almost perfeclty except for two things. The script use the following technique : tail -fn0 $logfile | \ while read line ; do ... some stuff done First one, I'd like a way to end the monitoring script if a... (3 Replies)
Discussion started by: Warluck
3 Replies

2. Shell Programming and Scripting

Log rotation script

I have an application that rotate its log once it reaches 100mb and it keeps a total of 24 logs. I am trying to write a script to run daily to tar up the previous day logs files and move them to a different directory. here is a long listing of the logs in the directory: -rw-r--r-- 1 user1 ... (6 Replies)
Discussion started by: e_mikey_2000
6 Replies

3. Shell Programming and Scripting

generate logfile in a shell script

Unix Gurus, I have a shell script which has few "echo" statements. I am trying to create a logfile where all the outputs of the echo statement sare stored. I will have to add this as the final step in the existing script so that everytime the script runs, a logfile is generated with all the... (1 Reply)
Discussion started by: shankar1dada
1 Replies

4. Shell Programming and Scripting

Script for Logfile Inserting into the Database

Here's the problem. I have a shell script running for every one minute as cronjob that outputs to a log file. I want to be able to take the data from the log file and insert it into a mysql database. The script needs to run at a set period of time and remove the log file data once the insert is... (7 Replies)
Discussion started by: kgrvamsi
7 Replies

5. Shell Programming and Scripting

Script to monitor errors in logfile

Hi, I would like to implements monitoring tool which looks at the logfile and sends me an email with error messages from the logfile. The runtime errors are printed in logfile. The logfile also contains some warning and debugging messages. For errors, the line will start with “Error”., the error... (1 Reply)
Discussion started by: LinuxLearner
1 Replies

6. Shell Programming and Scripting

Log rotation issue with script

I have application which to the heavy stdout and I have diverted the stdout to log file. this log file is writing very heavily and we have a script which rotates the logs. logic for rotation is smthing like cp logfile logfile.1 cat /dev/null > logfile this logic was working fine till we... (3 Replies)
Discussion started by: navinmistry
3 Replies

7. Shell Programming and Scripting

Logfile monitor script

Hi, I'm trying to write a logfile monitor script that reads the logfile and then emails out once there is an error with SQL in. Here is my attempt below which does not work. I'm not much of a scripter as you can probably see but some pointers in the right direction would be much appreciated. ... (3 Replies)
Discussion started by: elmesy
3 Replies

8. Shell Programming and Scripting

Attaching a logfile to the Script

Hello guys. I've recently written a basic utilities script just for home use. and i want to attach a logfile to it that will record all the commands that where executed in that script. Then just so i can add the d%b%y% and make each logfile unique and i can look back in each logfile to see what i... (9 Replies)
Discussion started by: matt02
9 Replies

9. Shell Programming and Scripting

Log rotation script

I have the below script to help with disk space cleanup that finds logs older than a specified number of days (say 10 days). I need it to grab "active" logs as well. Problem is an "active log" will not get archived unless I put in 0 days which I don't want to do, I need to leave the past 10 days,... (2 Replies)
Discussion started by: theninja
2 Replies

10. Shell Programming and Scripting

Log Rotation Tool/Script

Hello all Does anyone has an intersting script or a good freeware tool for log rotation that is good for Unix and Linux as well ? My thanks in advance (4 Replies)
Discussion started by: yelalouf
4 Replies
Login or Register to Ask a Question