linux系統部署)nohup.out 按日期自動進行日誌分割

nohup.out 日誌分割腳本 split.sh

#!/bin/bash
#author: zoriah
#create date: 2021-04-14
#description: nohup.out日誌分割
this_path=$(cd `dirname $0`;pwd)#根據腳本所在路徑
current_dte=`date -d "-1 day" "+%Y%m%d"`#列出時間
cd $this_path
echo $this_path
echo $current_date
do_split(){
	[ ! -d logs ] && mkdir -p logs
	split -b 10m -d -a 4 ./nohup.out ./logs/nohupsDirection/nohup-${current_date} #切分10兆每塊至logs文件中(自定義),格式爲:nohup-××××××××
	if [ $? -eq 0 ];
	then 
		echo "split is finished!"
	else 
		echo "split is failed!"
		exit 1 #退出shell,失敗
	fi
}
do_del_log(){
	find ./logs -type f -ctime +200 | xargs rm -rf #清理200天前創建的日誌
	cat /dev/null > nohup.out #清空當前目錄的nohup.out文件
}
if do_split;
then 
	do_del_log
	echo "nohup is split success!"
else
	echo "nohup is split failure!"
	exit 2 #退出shell,用法不當
fi
# crontab -e 添加定時任務,每天1點執行
#0 1 * * */1 /××××/split.sh &>/dev/null

方法步驟:

  1. 把split.sh放在nohup.out同路徑下;
  2. 進入定時任務:crontab -e,添加 0 1 * * */1 /××××/split.sh &>/dev/nul;
  3. 保存退出。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章