前言
为了避免手动删除文件夹内容,要求编写定时任务的脚本,实现每月1号、16号清除文件夹内文件。
准备资料
- clear_images.sh 脚本文件
#!/bin/bash
# 清除/opt/test/core/file目录下1天前所以jpg文件
find /opt/test/core/file -mtime +1 -type f -name "*.jpg" -exec rm -rf {} \;
详细步骤
- 进入核心代码文件夹 ,将脚本文件上传
cd /opt/test/core
mkdir cron
# 上传脚本文件
- 为脚本文件授权
chmod +x /opt/test/core/cron/clear_images.sh
- 使用crontab来设置定时任务 ,编辑crontab文件:
crontab -e
- 在crontab文件中添加以下内容:
# 每月1号、16号执行定时任务
0 0 1,16 * * /opt/test/core/cron/clear_images.sh