nginx_log="/home/sda_user/tail_test"
lost_nginx_log="/home/sda_user/tail_test/lost_log"
baklog_Dir="/home/sda_user/tail_test/bak_log"
pid_path=$nginx_log/"nginx.pid"
var=`date +%Y-%m-%d`
if [ ! -d ${lost_nginx_log} ]
then
mkdir -p ${lost_nginx_log}
fi
if [ ! -d ${baklog_Dir} ]
then
mkdir -p ${baklog_Dir}
fi
#先判断上次的切换日志的文件夹是否还有数据,如果有则不切。否则切换日志
if [[ -e $lost_nginx_log/access.log ]] && [[ -e $lost_nginx_log/n ]];
then
echo "not acces" >> /tmp/qxy.log
else
ps -ef | grep -i "/home/sda_user/tail_test/n" | grep -v grep | awk '{print $2}' | xargs kill -9
mv $nginx_log/access.log $lost_nginx_log/access.log
#kill -USR1 `cat ${pid_path}`
cp $nginx_log/n $lost_nginx_log/n
echo "1" > $nginx_log/n #切换日志后,将n置为1,从头开始读取新生成的日志。
#touch access.log
#启动数据重新读取。
nohup tail -n +$(tail -n1 $nginx_log/n) -F $nginx_log/access.log 2>&1 | awk 'ARGIND==1{i=$0;next}{i++;if($0~/^tail/){i=0};print $0;print i >> "/home/sda_user/tail_test/n";fflush("")}' $nginx_log/n - > /dev/null &
fi
nohup tail -n +$(tail -n1 $lost_nginx_log/n) -F $lost_nginx_log/access.log 2>&1 | awk 'ARGIND==1{i=$0;next}{i++;if($0~/^tail/){i=0};print $0;print i >> "/home/sda_user/tail_test/lost_log/n";fflush("")}' $lost_nginx_log/n - > /dev/null &
while [ 1 -eq 1 ]
do
old_size=`stat -c %s $lost_nginx_log/n`
sleep 20
new_size=`stat -c %s $lost_nginx_log/n`
if [[ "$old_size" = "$new_size" ]]; then
ps -ef | grep -i "/home/sda_user/tail_test/lost_log" | grep -v grep | awk '{print $2}' | xargs kill -9
break
fi
done
mv $lost_nginx_log/access.log $baklog_Dir/access$var.log
mv $lost_nginx_log/n $baklog_Dir/n$var.log
lost_nginx_log="/home/sda_user/tail_test/lost_log"
baklog_Dir="/home/sda_user/tail_test/bak_log"
pid_path=$nginx_log/"nginx.pid"
var=`date +%Y-%m-%d`
if [ ! -d ${lost_nginx_log} ]
then
mkdir -p ${lost_nginx_log}
fi
if [ ! -d ${baklog_Dir} ]
then
mkdir -p ${baklog_Dir}
fi
#先判断上次的切换日志的文件夹是否还有数据,如果有则不切。否则切换日志
if [[ -e $lost_nginx_log/access.log ]] && [[ -e $lost_nginx_log/n ]];
then
echo "not acces" >> /tmp/qxy.log
else
ps -ef | grep -i "/home/sda_user/tail_test/n" | grep -v grep | awk '{print $2}' | xargs kill -9
mv $nginx_log/access.log $lost_nginx_log/access.log
#kill -USR1 `cat ${pid_path}`
cp $nginx_log/n $lost_nginx_log/n
echo "1" > $nginx_log/n #切换日志后,将n置为1,从头开始读取新生成的日志。
#touch access.log
#启动数据重新读取。
nohup tail -n +$(tail -n1 $nginx_log/n) -F $nginx_log/access.log 2>&1 | awk 'ARGIND==1{i=$0;next}{i++;if($0~/^tail/){i=0};print $0;print i >> "/home/sda_user/tail_test/n";fflush("")}' $nginx_log/n - > /dev/null &
fi
nohup tail -n +$(tail -n1 $lost_nginx_log/n) -F $lost_nginx_log/access.log 2>&1 | awk 'ARGIND==1{i=$0;next}{i++;if($0~/^tail/){i=0};print $0;print i >> "/home/sda_user/tail_test/lost_log/n";fflush("")}' $lost_nginx_log/n - > /dev/null &
while [ 1 -eq 1 ]
do
old_size=`stat -c %s $lost_nginx_log/n`
sleep 20
new_size=`stat -c %s $lost_nginx_log/n`
if [[ "$old_size" = "$new_size" ]]; then
ps -ef | grep -i "/home/sda_user/tail_test/lost_log" | grep -v grep | awk '{print $2}' | xargs kill -9
break
fi
done
mv $lost_nginx_log/access.log $baklog_Dir/access$var.log
mv $lost_nginx_log/n $baklog_Dir/n$var.log
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22569416/viewspace-1977051/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22569416/viewspace-1977051/