centos 定时删除文件_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
————————————————
版权声明:本文为CSDN博主「weixin_39677538」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39677538/article/details/111943425

原文地址:https://www.cnblogs.com/telwanggs/p/15352720.html