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

    Cron 是 Linux 的内置服务,常用于处理定时任务,诸如每分钟同步一次指定状态,每天凌晨进行数据统计等都可以通过cron实现。

    cron最小执行单位是分钟。

    查看服务状态
    
    [vagrant@vagrant-c5-x86_64 ~]$ /sbin/service crond status
    
    crond (pid  2168) 正在运行...
    
    
    关闭服务
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo /sbin/service crond stop
    
    停止 crond:                                               [确定]
    
    
    启动服务
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo /sbin/service crond start
    
    启动 crond:                                               [确定]
    
    
    重启服务
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo /sbin/service crond restart
    
    停止 crond:                                               [确定]
    
    启动 crond:                                               [确定]
    
    
    重新载入配置
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo /sbin/service crond reload
    
    重新载入 cron 守护进程配置:                               [确定]
    
    
    添加为系统自启动
    
    /sbin/service crond start
    
    
    使用设定
    编辑某个用户的 cron 服务
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -e
    
    0 */1 * * * /usr/local/php/bin/php yingyongbao_push.php -t push_strategy
    
    
    查询某个用户 cron 服务详细内容
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -l
    
    0 */1 * * * /usr/local/php/bin/php yingyongbao_push.php -t push_strategy
    
    
    设定某个用户的 cron 服务 [一般root用户在执行这个命令的时候需要此参数]
    • eg. root 查看自己的 cron 设置
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo crontab -u root -l
    
    no crontab for root
    
    
    • eg. root 删除 vagrant 的 cron 设置
    
    [vagrant@vagrant-c5-x86_64 ~]$ sudo crontab -u vagrant -r
    
    
    删除某个用户的 cron 服务
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -e
    
    no crontab for vagrant - using an empty one
    
    crontab: installing new crontab
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -l
    
    0 */1 * * * /usr/local/php/bin/php yingyongbao_push.php -t push_strategy
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -r
    
    [vagrant@vagrant-c5-x86_64 ~]$ crontab -l
    
    no crontab for vagrant
    
    
    语法
    
    usage:	crontab [-u user] file
    
    	crontab [-u user] [ -e | -l | -r ]
    
    		(default operation is replace, per 1003.2)
    
    	-e	(edit user's crontab)
    
    	-l	(list user's crontab)
    
    	-r	(delete user's crontab)
    
    	-i	(prompt before deleting user's crontab)
    
    	-s	(selinux context)
    
    
    帮助
    
    [vagrant@vagrant-c5-x86_64 ~]$ man crontab
    
    

    基本格式

    • 列[各个字段之间用spaces和tabs分割]
    
    第1列 表示分钟 1~59 每分钟用 * 或者 */1 表示
    
    第2列 表示小时 1~23(0表示0点)
    
    第3列 表示日期 1~31
    
    第4列 表示月份 1~12
    
    第5列 标识号星期 0~6(0表示星期天)
    
    第6列 要运行的命令
    
    
    • 特殊符号

         *: 表示任何时刻
      
         ,: 表示分割
      
         -: 表示时间段,如1-5,表示1-5点
      
         /n: 表示每个n的单位执行一次,如 */1,表示每隔一个小时执行一次,等效于 1-23/1
      
        -1: 包含所有合法值,在月份域上使用 -1 意味着每个月都会触发这个 trigger
      
    • 例子

    
    #每晚的21:30
    
    30 21 * * * xxxx
    
    
    
    #每天早上6点10分
    
    10 6 * * * date
    
    
    
    #每两个小时
    
    0 */2 * * * date
    
    
    
    #每月 1、10、22日早上4点45分
    
    45 4 1,10,22 * * xxx
    
    
    
    #每周日,周二,周三的 17:00和 17:10 执行
    
    0,10 17 * * 0,2,3 
    
    
    
    #每隔10分 执行
    
    0,10,20,30,40,50 * * * * 
    
    */10 * * * * 
    
    
    
    
    #8:02,11:02,14:02,17:02,20:02 执行
    
    2 8-20/3 * * *
    
    

    /etc/crontab 文件

    存放系统运行的调度程序

    
    [vagrant@vagrant-c5-x86_64 etc]$ cat /etc/crontab
    
    SHELL=/bin/bash
    
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    
    MAILTO=root
    
    HOME=/
    
    
    
    # 完整配置示例
    
        root:以root身份执行文件夹下的所有脚本
    
        run-parts:执行命令路径
    
        /etc/cron.hourly:执行程序路径
    
        2>&1 表示所有的标准输出和错误输出都将被重定向到一个叫做 out.file 的文件中
    
        >/dev/null 2>&1 将错误输出2重定向到标准输出1,然后将标准输出1全部放到 /dev/null 文件,也就是清空
    
        > /data/xxx/xxx.log 2>&1 将错误输出2重定向到标准输出1,然后将标准输出1全部放到/data/xxx/xxx.log
    
    01 * * * * root run-parts /etc/cron.hourly
    
    00 01 * * * www /usr/bin/php /data/xxx/tools/test.php > /dev/null 2>&1
    
    

    /etc/cron.deny && /etc/cron.allow

    /etc/cron.deny 表示不能使用 crontab 命令的用户
    
    /etc/cron.allow 表示能使用crontab的用户
    
    如果两个文件同时存在,那么 /etc/cron.allow 优先
    
    如果两个文件都不存在,那么只有超级用户可以安排作业
    

    每个用户会在 /var/spool/cron 目录下 产生一个自己的 crontab 文件

    
    [root@vagrant-c5-x86_64 vagrant]# cd /var/spool/cron/
    
    [root@vagrant-c5-x86_64 cron]# ls
    
    vagrant
    
    [root@vagrant-c5-x86_64 cron]# cat vagrant
    
    0 */1 * * * /usr/local/php/bin/php yingyongbao_push.php -t push_strategy
    
    
  • 相关阅读:
    B.Icebound and Sequence
    Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS
    Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
    Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
    Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number
    Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
    Codeforces Round #561 (Div. 2) B. All the Vowels Please
    Codeforces Round #561 (Div. 2) A. Silent Classroom
    HDU-2119-Matrix(最大匹配)
    读书的感想!
  • 原文地址:https://www.cnblogs.com/biby/p/13826707.html
Copyright © 2011-2022 走看看