linux定时清理日志

来源:网络 点击数: 发布时间:2022年05月06日

问题描述:linux下如何定时清理日志文件?

1、mkdir crontab-file

创建定时清理日志脚本文件夹

2、touch clear_log.sh

创建定时日志脚本

3、vim clear_log.sh

编辑脚本文件

脚本:删除/etc/nginx/logs下30天之前的log文件

#!/bin/sh

 

find /etc/nginx/logs -mtime +30 -name "*.log" -exec rm -rf {} \;

4、赋予脚本执行权限

chmod 755 clear_log.sh

5、添加定时任务

crontab -l    :查看定时任务

crontab -e    :添加任务

crontab -r   :删除任务

每天00:30 定时执行/var/local/crontab-file/clear_log.sh脚本文件:

30 0 * * * /var/local/crontab-file/clear_log.sh

 

# Example of job definition: 定时任务示例

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

 

6、重启定时任务守护进程

systemctl restart crond