crontab 命令

  1、命令功能          

        通过 crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script 脚本。时 间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。

  这个命令非常适合周期性 的日志分析或数据备份等工作。
      2、安装crontab
   

        yum install crontabs
        服务操作说明:
        service crond start ## 启动服务
        service crond stop ## 关闭服务
        service crond restart ## 重启服务
        service crond reload ## 重新载入配置
        ## 查看 crontab 服务状态:
        service crond status
        ## 手动启动 crontab 服务:
        service crond start
        ## 查看 crontab 服务是否已设置为开机启动,执行命令:
         chkconfig --list
         ## 加入开机自动启动:
          chkconfig --level 35 crond on

      3、命令格式       

         crontab [-u user] file
         crontab [-u user] [ -e | -l | -r ]
         参数说明:
          -u user:用来设定某个用户的 crontab 服务,例如,“ -u ixdba”表示设定 ixdba 用户 的 crontab 服务,此参数一般有 root 用户来运行。
          file: file 是命令文件的名字,表示将 file 做为 crontab 的任务列表文件并载入 crontab。
         -e:编辑某个用户的 crontab 文件内容。如果不指定用户,则表示编辑当前用户的 crontab 文件。
          -l:显示某个用户的 crontab 文件内容,如果不指定用户,则表示显示当前用户的 crontab 文件内容。
          -r:删除定时任务配置,从/var/spool/cron 目录中删除某个用户的 crontab 文件,如 果不指定用户,则默认删除当前用户的 crontab 文件。
           -i:在删除用户的 crontab 文件时给确认提示。
          命令示例:
                 crontab file [-u user] ## 用指定的文件替代目前的 crontab。
         必须掌握:
               crontab -l [-u user] ## 列出用户目前的 crontab.
               crontab -e [-u user] ## 编辑用户目前的 crontab.

        4、配置说明:           

         基本格式 :
              * * * * * command
              分 时 日 月 周 命令
              第 1 列表示分钟 1~59 每分钟用*或者 */1 表示
              第 2 列表示小时 0~23( 0 表示 0 点) 7-9 表示: 8 点到 10 点之间
              第 3 列表示日期 1~31
              第 4 列表示月份 1~12
              第 5 列标识号星期 0~6( 0 表示星期天)
              第 6 列要运行的命令

        5、配置示例           

           */1 * * * * date >> /root/date.txt            上面的例子表示每分钟执行一次 date 命令
          30 21 * * * /usr/local/etc/rc.d/httpd restart         上面的例子表示每晚的 21:30 重启 apache
          45 4 1,10,22 * * /usr/local/etc/rc.d/httpd restart        上面的例子表示每月 1、 10、 22 日的 4 : 45 重启 apache
          10 1 * * 6,0 /usr/local/etc/rc.d/httpd restart          上面的例子表示每周六、周日的 1 : 10 重启 apache
          0,30 18-23 * * * /usr/local/etc/rc.d/httpd restart        上面的例子表示在每天 18 : 00 至 23 : 00 之间每隔 30 分钟重启 apache
          0 23 * * 6 /usr/local/etc/rc.d/httpd restart              上面的例子表示每星期六的 11 : 00 pm 重启 apache
          * */1 * * * /usr/local/etc/rc.d/httpd restart              上面的例子每一小时重启 apache
          * 23-7/1 * * * /usr/local/etc/rc.d/httpd restart         上面的例子晚上 11 点到早上 7 点之间,每隔一小时重启 apache
          0 11 4 * mon-wed /usr/local/etc/rc.d/httpd restart        上面的例子每月的 4 号与每周一到周三的 11 点重启 apache
          0 4 1 jan * /usr/local/etc/rc.d/httpd restart          上面的例子一月一号的 4 点重启 apache

  6、另外还有一些定时的开源工具:

    Oozie       Azkaban

posted on 2018-03-19 21:48  乱世浪子  阅读(140)  评论(0编辑  收藏  举报