zoukankan      html  css  js  c++  java
  • linux计划任务之cron

    cron计划任务之用户级

    • 1.安装crond
    centos7 执行命令:
    
    # yum install -y crontabs
    /bin/systemctl restart crond.service #重启服务
    /bin/systemctl status crond.service #查看crontab服务状态
    
    
    • 2.crond进程每分钟会处理一次计划任务 ,存储位置在 /var/spool/cron/
    • 3.管理方式
     crontab -l 列出当前用户的计划任务
     crontab -r 删除当前用户所有的计划任务
     crontab -e 编辑当前用户的计划任务
    

    管理员可以使用 -u username,去管理其他用户的计划任务
    示例

    [root@Server-n93yom ~]# crontab -e //编辑一个计划任务* * * * * date >> /root/tmp/huhu.job   后面的命令可以替换为一个脚本/*.sh
    [root@Server-n93yom ~]# crontab -l
    * * * * * date >> /root/tmp/huhu.job
    [root@Server-n93yom tmp]# tailf huhu.job
    Wed Sep 18 23:30:01 CST 2019
    [root@Server-n93yom tmp]# crontab -r
    [root@Server-n93yom tmp]# crontab -l
    no crontab for root
    
    • 4./etc/cron.deny 中定义的是禁止某用户去执行计划任务

    • 5.语法格式

    第一列代表分钟(0-59)
    
    第二列代表小时(0-23)
    
    第三列代表天(1-31)
    
    第四列代表月(1-12)
    
    第五列代表周(0-6) 0表示周日
    
    “*”代表所有的取值范围内的数字。如果直接用“*”则相当于“*/1”
    
    “/”代表每的意思
    
    “*/5″表示每5个单位
    
    “-”代表从某个数字到某个数字
    
    “,”分散的数字
    
     
    
    # 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
    
    # |  |  |  |  |
    
    # *  *  *  *  *  command to be executed
    
    

    cron计划任务之系统级

    • 系统计划任务的作用
      - 临时文件的清理 /tmp /var/tmp
      - 系统信息的采集
      - 日志的轮转 (切割)logrotate
    • cron系统任务文件:/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
    
    
    * * * * * root /etc/cron.hourly/logstash_heartbeat
    
    • 系统任务文件:ls /etc/cron.d/* //crond仅仅会执行每小时定义的脚本 ; /etc/cron.hourly 该文件夹下有个0anacron进程,每小时的01分钟会唤醒anacrond进程
      - anacrond详解及命令
    [root@Server-n93yom ~]# ls /etc/cron.d/*
    /etc/cron.d/0hourly
    [root@Server-n93yom ~]# cat /etc/cron.d/0hourly
    # Run the hourly jobs
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    01 * * * * root run-parts /etc/cron.hourly
    [root@Server-n93yom ~]# ls /etc/cron.hourly
    0anacron  logrotate  logstash_heartbeat
    [root@Server-n93yom ~]# ls /var/spool/anacron/
    cron.daily  cron.monthly  cron.weekly                   //anacron下的计划任务
    [root@Server-n93yom ~]# cat /etc/anacrontab    //查询anacrontab
    # /etc/anacrontab: configuration file for anacron
    
    # See anacron(8) and anacrontab(5) for details.
    
    SHELL=/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    # the maximal random delay added to the base delay of the jobs
    RANDOM_DELAY=45
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=3-22
    
    #period in days   delay in minutes   job-identifier   command          //每小时的01分anacron进程会检查每天,每周或者每月的定时任务,若之前因为停机异常等导致的错过的任务,会后续补上
    1	5	cron.daily		nice run-parts /etc/cron.daily             //每天的任务若错过,则会在整点延后五分钟补上上次遗漏的任务
    7	25	cron.weekly		nice run-parts /etc/cron.weekly  //每周的任务若错过,则会在整点延后25分钟补上上次遗漏的任务
    @monthly 45	cron.monthly		nice run-parts /etc/cron.monthly  //每月的任务若错过,则会在整点延后25分钟补上上次遗漏的任务
    
    • cron日志
    [root@Server-n93yom ~]# tailf /var/log/cron
    Sep 19 23:01:01 Server-n93yom run-parts(/etc/cron.hourly)[1355]: finished 0anacron
    Sep 19 23:01:01 Server-n93yom run-parts(/etc/cron.hourly)[1346]: starting logrotate
    Sep 19 23:01:02 Server-n93yom run-parts(/etc/cron.hourly)[1362]: finished logrotate
    Sep 20 00:01:01 Server-n93yom CROND[5576]: (root) CMD (run-parts /etc/cron.hourly)
    Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5576]: starting 0anacron
    Sep 20 00:01:01 Server-n93yom anacron[5585]: Anacron started on 2019-09-20
    Sep 20 00:01:01 Server-n93yom anacron[5585]: Normal exit (0 jobs run)
    Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5587]: finished 0anacron
    Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5576]: starting logrotate
    Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5594]: finished logrotate
    
  • 相关阅读:
    T4设计时模板调试
    MVC开发T4代码生成之一文本模板基础
    经典选项卡
    IE 调试工具 Utilu IE Collection:IE5.5、6.0、7.0, 8.0…各版本浏览器兼容性测试工具
    滚动读行!
    自定义标签的用法、支持IE6
    jQuery 参数传递例子
    IMG在IE6下不能HOVER的解决办法!
    点击渐变弹出层
    操作滚动条滚动到指定位置
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/11546254.html
Copyright © 2011-2022 走看看