美文网首页
Crontab命令详解

Crontab命令详解

作者: garyond | 来源:发表于2017-01-03 10:57 被阅读233次

    Crontab命令使用

    crontab 是用来让使用者(用户)在固定时间或固定间隔(即既定时间设置内)执行应用程序或代码。

    • crontab file [-u user] -用指定的文件替代目前的crontab.
    • crontab -[-u user] -用标准输入替代目前的crontab.
    • crontab -1[user] -列出用户目前的crontab.
    • crontab -e[user] -编辑用户目前的crontab.
    • crontab -d[user] -删除用户目前的crontab.
    • **crontab -c dir ** -指定crontab的目录。

    命令示例:

    • 查看当前用户的定时任务

      [kettle@localhost sh]$ crontab –l

    • 编辑当前用户的定时任务

      [kettle@localhost sh]$ crontab –e

    • 编辑当前用户的定时任务

      [kettle@localhost sh]$ crontab –r

    Crontab命令时间格式设置

     *  *  *  *  *  command 
     分  时  日  月  周  命令 
    
    • 第1列表示分钟1~59 每分钟用*或者 */1表示
    • 第2列表示小时1~23(0表示0点)
    • 第3列表示日期1~31
    • 第4列表示月份1~12
    • 第5列标识号星期0~6(0表示星期天)
    • 第6列要运行的命令
    Crontab命令

    Crontab定时任务示例

    30 21 * * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每晚的21:30重启apache。

    45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每月1、10、22日的4 : 45重启apache。

    10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每周六、周日的1 : 10重启apache。

    0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

    0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每星期六的11 : 00 pm重启apache。

    • */1 * * * /usr/local/etc/rc.d/lighttpd restart

    每一小时重启apache

    • 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart

    晚上11点到早上7点之间,每隔一小时重启apache

    0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart

    每月的4号与每周一到周三的11点重启apache

    0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart

    一月一号的4点重启apache

    0 7 * * * /bin/ls

    每天早上7点执行一次 /bin/ls

    0 6-12/3 * 12 * /usr/bin/backup

    在 12 月内, 每天的早上 6 点到 12 点中,每隔3个小时执行一次 /usr/bin/backup

    0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata

    周一到周五每天下午 5:00 寄一封信给 alex@domain.name

    20 0-23/2 * * * echo "haha"

    每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"

    相关文章

      网友评论

          本文标题:Crontab命令详解

          本文链接:https://www.haomeiwen.com/subject/tnwgvttx.html