Linux任务计划、周期性任务执行

Linux任务计划、周期性任务执行

    周期性任务执行: cron

    守护进程(crond):服务,不间断地运行于后台

    # service crond {start|stop|status|restart}

    cron的任务分两类:
        系统cron任务:定义在/etc/crontab文件中
        用户cron任务:定义在/var/spool/cron/USERNAME文件中;功能类似于/etc/crontab文件

        每行定义一个周期性任务计划

            SHELL=/bin/bash
            PATH=/sbin:/bin:/usr/sbin:/usr/bin
            MAILTO=root
            HOME=/   
           
                # 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   
               
            例如:在7点8分运行echo "how are you?"
                08 07 * * * root /bin/echo "how are you?"   

            结果通过邮件进行通知;

        时间表示法:
            时间:
                分钟:有效取值0-59
                小时:有效取值0-23
                天:1-31
                月:1-12
                周几:0-7
                    0,7都表示周日

            (1) 给出其有效取值范围内的某特定值;
                5 * * * * /bin/echo "howdy" :
                    *的取值范围为所有,即每小时的第五分钟执行命令,用户名可以省略
               
                16 8 * * * /bin/echo "howdy"
                    每天的第8小时的第16分钟执行命令
               
                16 9 * * 3 /bin/echo "howdy"
                    每周三的9点16分执行命令
                   
                12 11 20 * * /bin/echo "howdy"
                    每月的第20天的第11小时的第12分钟执行命令
                   
                03 07 01 07 * /bin/echo "howdy"
                    每年的7月1号的7点03分执行命令
                   
            (2) *:给定时间位上的有效取值范围内的所有可用值
           
            (3) -: 给定时间位连续取值区间
                6-12 * * * * /bin/echo "howdy"
                    每小时的第六分钟到12分钟,每分钟执行一次
                        即第6,7,8,9,10,11,12分钟分别执行一次
                   
            (4) ,: 给定时间位上的离散取值
                1,31 * * * * /bin/echo "howdy"
                1,31 6,18 * * * /bin/echo "howdy"
               
            (5) */#: 对应时间位每#个时间单位一次;
                1 2 */3 * * /bin/echo "howdy"
                    每三天的第2小时第1分钟执行一次命令,前边的小时和分钟必须是规定的,而不是*
               
                */3 * * * /bin/echo "howdy"
                    每三分钟执行一次

posted @ 2015-10-14 18:31  lucky_zhang  阅读(1257)  评论(0编辑  收藏  举报