Rotate the logs

Rotate the logs

am 01.02.2008 22:56:19 von laxman

Hi everybody,


i am posting a long script which will remove logs when it reaches 15
in number...
Default it will create stdout.log ..i have copied this file to
stdout.log0 and moving the logs ...when it turns to stdout.log15 the
log will be removed .....so i want to make it as a short
script .......please help me


#!/bin/sh
# To Rotate the logs
#*********************************************************** **

#*********************************************************** **
#* initializing the variables
#* variable "a" is used to store the size of the stdout.log file
#* variable "b" is used to store the number of stdout.log files
#*********************************************************** **
a=0
integer b=0
integer i=0
j=0

#*********************************************************** **
#* Script To find the size of the stdout.log file
#*********************************************************** **
du -k stdout.log >test
a=`awk '{print $1}' test `

#*********************************************************** **
#* Script To find the number of stdout.log files present
#*********************************************************** **
b=`ls stdout.log*|wc -l`

#*********************************************************** **
#* The If statement to determine the size of the log file
#*********************************************************** **
if [ $a -gt 10 ]
then

#*********************************************************** **
#* Script to rotate the log files .Case statement is used to
#* switch the control according to the number of log files
#*********************************************************** **
case "$b" in

1) cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null >stdout.log;e[i]=$?
for j in 0 1
do
if [${e[i]} -eq 0 ]
then
echo "Success"
else
echo "failed"
fi
done

;;

2) mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null >stdout.log;e[i]=$?
for j in 0 1 2
do
if [${e[i]} -eq 0 ]
then
echo "Success"
else
echo "failed"
fi
done
;;

3) mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null >stdout.log;e[i]=$?
for j in 0 1 2 3
do
if [${e[i]} -eq 0 ]
then
echo "Success"
else
echo "failed"
fi
done

;;

4) mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?

for j in 0 1 2 3 4
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

5) mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?
for j in 0 1 2 3 4 5
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

6) mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?
for j in 0 1 2 3 4 5 6
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

7) mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?
for j in 0 1 2 3 4 5 6 7
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

8) mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?

for j in 0 1 2 3 4 5 6 7 8
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done
;;

9) mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

10) mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?
for j in 0 1 2 3 4 5 6 7 8 9 10
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

11) mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

12) mv stdout.log10 stdout.log11;e[i]=$?;i=i+1
mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

13) mv stdout.log11 stdout.log12;e[i]=$?;i=i+1
mv stdout.log10 stdout.log11;e[i]=$?;i=i+1
mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done


;;

14) mv stdout.log12 stdout.log13;e[i]=$?;i=i+1
mv stdout.log11 stdout.log12;e[i]=$?;i=i+1
mv stdout.log10 stdout.log11;e[i]=$?;i=i+1
mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

15) mv stdout.log13 stdout.log14;e[i]=$?;i=i+1
mv stdout.log12 stdout.log13;e[i]=$?;i=i+1
mv stdout.log11 stdout.log12;e[i]=$?;i=i+1
mv stdout.log10 stdout.log11;e[i]=$?;i=i+1
mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

16) rm -rf stdout.log14;e[i]=$?;i=i+1
mv stdout.log13 stdout.log14;e[i]=$?;i=i+1
mv stdout.log12 stdout.log13;e[i]=$?;i=i+1
mv stdout.log11 stdout.log12;e[i]=$?;i=i+1
mv stdout.log10 stdout.log11;e[i]=$?;i=i+1
mv stdout.log9 stdout.log10;e[i]=$?;i=i+1
mv stdout.log8 stdout.log9;e[i]=$?;i=i+1
mv stdout.log7 stdout.log8;e[i]=$?;i=i+1
mv stdout.log6 stdout.log7;e[i]=$?;i=i+1
mv stdout.log5 stdout.log6;e[i]=$?;i=i+1
mv stdout.log4 stdout.log5;e[i]=$?;i=i+1
mv stdout.log3 stdout.log4;e[i]=$?;i=i+1
mv stdout.log2 stdout.log3;e[i]=$?;i=i+1
mv stdout.log1 stdout.log2;e[i]=$?;i=i+1
mv stdout.log0 stdout.log1;e[i]=$?;i=i+1
cp stdout.log stdout.log0;e[i]=$?;i=i+1
cat /dev/null > stdout.log;e[i]=$?;i=i+1
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 13 15 16
do
if [ ${e[j]} -eq 0 ]
then
echo "success"
else
echo "failed"
fi
done

;;

#*********************************************************** **
#* End of case statement
#*********************************************************** **
esac
else
echo "Size not greater than specified"

#*********************************************************** **
#* End of IF statement
#*********************************************************** **
fi

Thanks in Advance

Re: Rotate the logs

am 01.02.2008 23:38:07 von Ed Morton

On 2/1/2008 3:56 PM, laxman wrote:
> Hi everybody,
>
>
> i am posting a long script which will remove logs when it reaches 15
> in number...
> Default it will create stdout.log ..i have copied this file to
> stdout.log0 and moving the logs ...when it turns to stdout.log15 the
> log will be removed .....so i want to make it as a short
> script .......please help me

man logrotate

Ed.