2018/01/25

January 25 2018 Thursday

Weather : light rain ;
一、需求:
系統logrotate工具能夠完成日誌切割、歸檔。寫一個shell腳本實現相似的歸檔功能。
舉例:加入服務的輸出日誌是1.log,要求天天歸檔一個,1.log次日就變成1.log.1,第三天1.log.2,第四天1.log.3 一直到1.log.5shell

[aming@Dasoncheng ~]$ cat g.sh 
#!/bin/bash
function log_rotate()
{
  [ -f $1 ] && rm -f $1
}

for i in `seq 5 -1 2`;
do
  i2=$[ $i - 1 ]
  log_rotate 1.log.$i
  if [-f 1.log.$i2];
  then
      mv 1.log.$i2 1.log.$i
  fi
done

log_rotate 1.log.1
mv 1.log 1.log.1
##attation:it should restart the service,i ignored this time !
本站公眾號
   歡迎關注本站公眾號,獲取更多信息