使用shell定时执行脚本

yum install crontabs //安装

 

#检查安装
crontab -V

 

#服务操作
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //服务状态

 

查看配置文件
cat /etc/crontab

 

查看已经配置的定时任务

crontab -l

 

编辑新定时任务

crontab -e

 

定时任务规则

f1 f2 f3 f4 f5 program
  • 其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
  • 当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
  • 当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
  • 当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
  • 当 f1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,f2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,其馀类推
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)



编写测试

#!/bin/sh
echo $(date) >> /root/test1.txt

 

每分钟将时间写入 /root/test1.txt

* * * * * /root/test.sh

chmod 777 /root/test.sh

 

获取七天前时间

$(date -d -7day +%Y_%m_%d)

获取当前时间

 $(date +%y%m%d) 

posted @ 2022-07-25 14:39  冰糖狐狸  阅读(4071)  评论(0编辑  收藏  举报