I'm writing a perl script that would help me to learn howto create a file log rotator that will simulate the real /var/log/messages. This script checks and creates these files: messages, messades1, messages2, messages3, messages4.
The first one represents the current log and the others are older versions. Each time the script runs it should check for the existance of messages.3 and, if it exists, move it to messages.4. Then is should check for messages.2 and move it to messages.3 and so on, until messages is moved to messages1,
This is the script:
#!/usr/bin/perl
open(LOG,">messages");
$logfile = $ARGV[0] ;
$to_num = $ARGV[5] ;
while($to_num > 1)
{
$from_num = $to_num - 1 ;
if(-f "$logfile.$from_num")
{
system("/bin/mv $logfile.$from_num $logfile.$to_num") ;
}
$to_num-- ;
}
if(-f $logfile)
system("/bin/cp $logfile $logfile.1") ;
system("/bin/cp /dev/null $logfile") ;
}
and finally it migth be run once a week by cron to help organize the log files.
my problem is that the script is not working.I am confused about how to create the 5 messages files at the same time(files that are created every time the script is running), and I know it has a few mistakes but as a perl newbie I can't get this script working. Does anybody help me?,
please
Many thanks...
The first one represents the current log and the others are older versions. Each time the script runs it should check for the existance of messages.3 and, if it exists, move it to messages.4. Then is should check for messages.2 and move it to messages.3 and so on, until messages is moved to messages1,
This is the script:
#!/usr/bin/perl
open(LOG,">messages");
$logfile = $ARGV[0] ;
$to_num = $ARGV[5] ;
while($to_num > 1)
{
$from_num = $to_num - 1 ;
if(-f "$logfile.$from_num")
{
system("/bin/mv $logfile.$from_num $logfile.$to_num") ;
}
$to_num-- ;
}
if(-f $logfile)
system("/bin/cp $logfile $logfile.1") ;
system("/bin/cp /dev/null $logfile") ;
}
and finally it migth be run once a week by cron to help organize the log files.
my problem is that the script is not working.I am confused about how to create the 5 messages files at the same time(files that are created every time the script is running), and I know it has a few mistakes but as a perl newbie I can't get this script working. Does anybody help me?,
please
Many thanks...