创建shell脚本
#创建文件
vim delete.sh
#写入需要执行的命令
#!/bin/bash
echo '清除文件开始';
h1=$(date "+%Y-%m-%d %H:%M:%S");
echo "当前时间: ${h1}"
find /opt/test -mtime -1 -name "*_23-30-01.tar.gz" -exec rm -Rf {} \;
echo '清除文件结束'
将脚本变为可执行文件
chmod +x ./delete.sh
创建定时任务
#创建定时任务
crontab -e
#在文件配置
59 * * * * /opt/delete.sh > /opt/test/2.txt
日期解释
* | * | * | * | * | command |
---|---|---|---|---|---|
分钟(0-59) | 小时(0-23) | 日期(1-31) | 月份(1-12) | 星期(1-7) | 执行命令 |
查看已创建的定时任务
[root@node1 opt]# crontab -l
59 * * * * /opt/delete.sh > /opt/test/2.txt
crontab 常用命令
命令 | 解释 |
---|---|
crontab -u | 指定用户的cron信息 |
crontab -l | 列出当前用户下的cron服务的信息 |
crontab -u user -l | 列出指定用户的cron服务的信息 |
crontab -r | 删除cron服务 |
crontab -e | 编辑cron服务 |
crontab -r -u user | 删除指定用户的定时任务 |