Centos 定时清理文件(Shell )

  • A+
所属分类:Centos Shell

服务器每天都会定时备份数据库和文件,时间久了导致磁盘空间不足。所以需要用shell脚本实现定时清理文件。

创建shell文件 clear_tmp_file

#  vi clear_tmp_file.sh

写入清理命令

#!/bin/sh

find /home/tmp/log -mtime +2 -name ".bckup" -exec -rm rf {}\;

说明:

/home/tmp/log: 要删除的文件所在的目录

-mtime +2 : 文件保留天数,即几天前的文件会被删除

-name ".backup": 以".backup" 为后缀的文件

其他为固定字符

保存文件并给文件授权

# chown 777 clear_tmp_file.sh

利用crontab 创建定时器

输入“crontab -e”进行任务创建页面

0 4 * * * clear_tmp_file.sh

启动定时器

在主界面输入 

# /bin/systemctl  start crond.service

说明:

启动定时任务:  /bin/systemctl start crond.service

停止定时任务:  /bin/systemctl stop crond.service

重启定时任务: /bin/systemctl restart crond.service

查看定时任务状态:  /bin/systemctl status  crond.service

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: