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
方法步骤:
- 把split.sh放在nohup.out同路径下;
- 进入定时任务:crontab -e,添加 0 1 * * */1 /××××/split.sh &>/dev/nul;
- 保存退出。