linux系统定时任务定期清楚nohup日志

项目开发中,经常会使用nohup 命令启动应用,但项目产生日志过多,没几天硬盘空间就满了,设置一个定期任务清理日志则能解决次问题,操作如下

1.编写执行脚本

#!/bin/bash
# clean the nohup.out of book ,info,server
# date:2020-08-13

path1="/weblogic/stock_domain"
path2="/weblogic/bbsbook_domain"
path3="/weblogic/bbsinfo_domain"
if [ $# -eq 0 ];then
cd $path1
file_size=`du -sm nohup.out | awk '{print $1}'`

if [ $file_size -ge 1 ];then
/bin/cp /dev/null nohup.out
echo "$(date +%F' '%X) :The file size $file_size M , clean it over!" >> /weblogic/stock_domain/clean_nohup.out.log
else
echo "$(date +%F' '%X) :The file size $file_size M , not clean it !" >> /weblogic/stock_domain/clean_nohup.out.log
fi
cd $path2
file_size=`du -sm nohup.out | awk '{print $1}'`

if [ $file_size -ge 1 ];then
/bin/cp /dev/null nohup.out
echo "$(date +%F' '%X) :The file size $file_size M , clean it over!" >> /weblogic/bbsbook_domain/clean_nohup.out.log
else
echo "$(date +%F' '%X) :The file size $file_size M , not clean it !" >> /weblogic/bbsbook_domain/clean_nohup.out.log
fi
cd $path3
file_size=`du -sm nohup.out | awk '{print $1}'`

if [ $file_size -ge 1 ];then
/bin/cp /dev/null nohup.out
echo "$(date +%F' '%X) :The file size $file_size M , clean it over!" >> /weblogic/bbsinfo_domain/clean_nohup.out.log
else
echo "$(date +%F' '%X) :The file size $file_size M , not clean it !" >> /weblogic/bbsinfo_domain/clean_nohup.out.log
fi
else
echo "Don't take parameters !"
fi

--------------------------------------------------------------------

备注:path1,path2,path3 路径 为需要清理nohup.out日志的三个路径

2. crontab -e 创建定时任务

添加以下内容


59 11 * * * ./etc/profile;sh /weblogic/stock_domain/ClearNohup.sh

原文地址:https://www.cnblogs.com/liuzhenguo/p/13498550.html