zoukankan      html  css  js  c++  java
  • 定时任务crontab

    定时任务-crontab

    命令

    crontab [-u user] file
    crontab -e
    crontab -l
    crontab -r
    crontab -i
    
    参数:
     -e 编辑定时任务  操作和vim一样
     -l 查看定时任务列表  
     -r 删除定时任务; 从 /var/spool/cron 目录中,删除某个用户的 crontab 文件,如果不指定用户,则默认删除当前用户的 crontab 文件
     -i 在删除用户的crontab文件时给确认提示
    

    crontab 文件格式

    1、格式

    第1列:分钟,0~59
    第2列:小时,0~23
    第3列:日期,1~31
    第4列:月份,1~12
    第5列:星期,0~7(0和7表示星期天)
    第6列:要运行的命令(如果有多个命令用 && 隔开)

    亦可以通过cat /etc/crontab 查看系统给的提示

    [root@localhost cron]# cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    You have new mail in /var/spool/mail/root
    
    

    2、特殊字符

    星号(*) :代表所有可能的值,例如,month字段如果是星号,则表示在满足其它字段的制约条件后,每月都执行该命令操作;

    逗号(,) :可以用逗号隔开的值,指定一个多元素的列表,例如:1,2,5,7,8,9

    中杠(-) :可以用整数之间的中杠表示一个整数范围,例如“2-6”表示:2,3,4,5,6

    正斜线(/) :可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

    示例:

    1、查看当前用户的当前定时任务

    表示一分钟执行一次 输出hello world

    [root@localhost cron]# crontab -l
    */1 * * * *  echo "hello world"
    You have new mail in /var/spool/mail/root
    [root@localhost cron]#
    
    

    2、编辑定时任务:

    [root@localhost cron]# crontab -e
    
    */1 * * * *  echo "hello world"
    0 10 * * * /bin/sh /home/b.sh  表示每天10天执行指定的脚本
    

    3、删除定时任务

    不要轻易执行,这个命令删除当前用户的所有定时任务

    [root@localhost cron]# crontab -r
    [root@localhost cron]# crontab -l
    no crontab for root
    [root@localhost cron]#
    
    

    4、提示是否确定删除 -i

    [root@localhost cron]# crontab -ir
    crontab: really delete root's crontab?
    

    crontab语法:

    [root@localhost cron]# cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    You have new mail in /var/spool/mail/root
    

    备注:
    1) * 表示任意的(分、时、日、月、周)时间都执行
    2) - 表示一个时间范围段, 如5-7点
    3) , 表示分隔时段, 如6,0,4表示周六、日、四
    4) /1 表示每隔n单位时间, 如*/10 每10分钟 表示频率

    练习

    00 02 * * * ls #每天的凌晨2点整执行ls命令
    00 02 1 * * ls #每月的1日的凌晨2点整执行
    00 02 14 2 * ls #每年的2月14日凌晨2点执行
    00 02 * * 7 ls #每周天的凌晨2点整执行
    00 02 * 6 5 ls #每年6月每个周五凌晨2点执行
    00 02 14 * 7 ls #每月14日或每周日的凌晨2点都执行
    00 02 14 2 7 ls #每年的2月14日或每年2月的周天的凌晨2点执行
    */10 02 * * * ls #每天凌晨2点,每隔10分钟执行一次
    * * * * * ls #每分钟都执行
    00 00 14 2 * ls #每年2月14日的凌晨执行命令
    */5 * * * * ls #每隔5分钟执行一次
    00 02 * 1,5,8 * ls #每年的1月5月8月凌晨2点执行
    00 02 1-8 * * ls # 每个月1-8号凌晨2点都执行
    0 21 * * * ls #每天晚上21:00执行
    45 4 1,10,22 * * ls #每月的1,10,22号的4:45执行
    45 4 1-10 * * ls #每月的1到10号的4:45执行
    3,15 8-11 */2 * * ls #每隔两天的上午8点到11点的第3和第15分钟执行
    0 23-7/1 * * * ls #晚上11点到早上7点之间,每隔一个小时执行
    15 21 * * 1-5 ls #周一到周五每天晚上21:15执行
    

    crontab 相关命令
    crontab -e 编辑定时任务
    crontab -l 查看定时任务
    crontab -r 删除定时任务
    crontab -u 指定其他用户

  • 相关阅读:
    Egret Inspector google 插件 浏览器报错的解决方案:
    egret 项目 支持 es6 解决方案
    promise-abortable
    BaseSocket
    【译】async/await 优点、陷阱以及如何使用 (经验总结)
    WebSocket 断线重连引入心跳的原因
    node js 项目: 采用typescript 编写的好文章
    创建Node.js TypeScript后端项目 demo
    vs code 提高工作效率的办法:
    cocos2d-x 贝塞尔曲线(Bezier)用法详解
  • 原文地址:https://www.cnblogs.com/liangyongwang/p/15632416.html
Copyright © 2011-2022 走看看