1.下載logrotate
在Fedora和CentOS安裝
yum install logrotate crontabs
Debian和Ubuntu上
apt-get install logrotate cron
2.創建日志文件在文件里面添加日志
vim /var/log/file.log
3.創建file.log的配置文件添加
vim /etc/logrotate.d/file
/vsr/log/file.log{
daily 日志文件按每個月輪循 daily 每天 weakly 每星期 yealy 每年
rotate 5 儲存5個歸檔日志,對於第六個歸檔,時間最久的自動刪除
compress 以歸檔的文件用gzip進行壓縮
mail address 把轉儲的日志文件發送到指定的E-mail 地址
nocompress 不需要壓縮時,用這個參數
copytruncate 用於還在打開中的日志文件,把當前日志備份並截斷
nocopytruncate 備份日志文件但是不截斷
create 644 root root 轉儲文件,使用指定的文件模式創建新的日志文件
size 20M 指定文件當天達到20M的時候才轉儲
dateext 文件后加上當前日期
}
以上的參數可以根據自己的需求而添加
4.手動切割日志
logrotate -vf /etc/logrotate.d/file
5.在/var/log/ 目錄下你會看到分割的日志文件

6.還可以使用crontab定時寫入數據到file.log
首先檢查crontab是否啟動
檢查crontab服務是否啟動
service crond status
啟動crontab服務
service crond start
7.使用命令 crontab -e 命令進入編輯模式定時寫入數據到file.log
#cron 格式 */1 * * * * echo"ni hao nihao" >>/var/log/file.log #分鍾(0~59) 小時(0~23) 幾號(1~31) 幾月(1~12) 星期幾(0~7) 命令 #每行代表都代表一個任務 #上面代碼意思是每分鍾向文件file.log文件寫入 nihao nihao
8.查看當前任務
crontab -l
9.查看定時任務的數據是否寫入文件
tail -f /var/log/file.log
