当前位置: 移动技术网 > 科技>操作系统>Linux > Linux 定时清理日志脚本

Linux 定时清理日志脚本

2018年12月29日  | 移动技术网科技  | 我要评论

在远程运行节点创建一个cleanlog.sh 脚本文件
vin clenalog.sh 插入以下内容

#!/bin/env bash

start=$(date +%y-%m-%d-%h%m%m)
file=/software/dubbo-monitor-data/cleanlog/clean_log_$start.txt
filepath=/software/dubbo-monitor-data/statistics

echo $file

if [ ! -n "$1" ];
then
    day=7
else
    day=$1
fi

#删除7天之前的文件夹
echo "输出日志文件:"
echo "find $filepah -type d -ctime +$day > $file"
find $filepath -type d -ctime +$day > $file

echo "删除日志文件:"
echo "find $filepath -type d -ctime +$day | xargs rm -rf"
find $filepath  -type d -ctime +$day | xargs rm -rf

添加定时任务,设置每天凌晨3点远程执行192.168.1.1节点的脚本清理日志(192.168.1.1这个ip是我临时写的,运行时修改为自己的ip)

0 3 * * * ssh root@192.168.1.1 "/software/dubbo-monitor-data/cleanlog/cleanlog.sh >/dev/null 2>&1"

我这个定时任务这么写是因为环境限制(需要给远程服务器添加密钥验证,否则无法执行)如果可以建议把定时任务运行在需要清理日志的服务器上

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网