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

    一、at : 未来的某时间点执行一次任务

    at命令:at [option] TIME

    常用选项:
    -V 显示版本信息:
    -l: 列出指定队列中等待运行的作业;相当于atq
    -d: 删除指定的作业;相当于atrm
    -c: 查看具体作业任务
    -f /path/from/somefile:从指定的文件中读取任务
    -m:当任务被完成之后,将给用户发送邮件,即使没有标准输出

    TIME:定义出什么时候进行 at 这项任务的时间
    HH:MM [YYYY-mm-dd]
    noon, midnight, teatime(4pm)
    tomorrow
    now+#{minutes,hours,days, OR weeks}

    HH:MM 02:00
    在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
    HH:MM YYYY-MM-DD 02:00 2016-09-20
    规定在某年某月的某一天的特殊时刻进行该项任务
    HH:MM[am|pm] [Month] [Date]
    04pm March 17
    17:20 tomorrow
    HH:MM[am|pm] + number [minutes|hours|days|weeks]
    在某个时间点再加几个时间后才进行该项任务
    now + 5 minutes
    02pm + 3 days

    执行方式:
    1)交互式 2)输入重定向 3)at –f 文件
    依赖与atd服务,需要启动才能实现at任务
    at队列存放在/var/spool/at目录中
    /etc/at.{allow,deny}控制用户是否能执行at任务
    白名单:/etc/at.allow 默认不存在,只有该文件中的用户才能执行at命令
    黑名单:/etc/at.deny 默认存在,拒绝该文件中用户执行at命令,而没有在at.deny 文件中的使用者则可执行
    如果两个文件都不存在,只有 root 可以执行 at 命令

    二、cron:周期性任务计划

    依赖包:cronie
    cronie-anacron:cronie的补充程序,用于监控cronie任务执行状况,如cronie中的任务在过去该运行的时间点未能正常运行,则anacron会随后启动一次此任务
    crontabs:包含CentOS提供系统维护任务
    系统默认这三个包都会安装

    确保crond守护处于运行状态
    CentOS 7:
    systemctl status crond
    CentOS 6:
    service crond status

    计划周期性执行的任务提交给crond,到指定时间会自动运行

    cron的系统配置文件
    /etc/crontab
    /etc/cron.d/ 配置文件
    /etc/cron.hourly/ 脚本
    /etc/cron.daily/ 脚本
    /etc/cron.weekly/ 脚本
    /etc/cron.monthly/ 脚本
    运行计算机关机时cron不运行的任务,CentOS6以后版本取消anacron服务,由crond服务管理
    对笔记本电脑、台式机、工作站、偶尔要关机的服务器及其它不一直开机的系统很重要对很有用
    配置文件:/etc/anacrontab,负责执行/etc/ cron.daily /etc/cron.weekly /etc/cron.monthly中系统任务。

    我们来看一下anacrontab这个配置文件

    1. [root@newhostname etc]# cat /etc/anacrontab
    2. # /etc/anacrontab: configuration file for anacron
    3. # See anacron(8) and anacrontab(5) for details.
    4. SHELL=/bin/sh
    5. PATH=/sbin:/bin:/usr/sbin:/usr/bin
    6. MAILTO=root
    7. # the maximal random delay added to the base delay of the jobs
    8. RANDOM_DELAY=45 #一个随机的时间作为延时,以为了避免繁忙的工作时间执行
    9. # the jobs will be started during the following hours only
    10. START_HOURS_RANGE=3-22 #排除时间,表示哪个时间段不执行
    11. #period in days delay in minutes job-identifier command
    12. 1 5 cron.daily nice run-parts /etc/cron.daily
    13. 7 25 cron.weekly nice run-parts /etc/cron.weekly
    14. @monthly 45 cron.monthly nice run-parts /etc/cron.monthly

    最后面字段的意思
    •字段1:如果在这些日子里没有运行这些任务……
    •字段2:在重新引导后等待这么多分钟后运行它
    •字段3:任务识别器,在日志文件中标识
    •字段4:要执行的任务
    由/etc/cron.hourly/0anacron执行
    当执行任务时,更新/var/spool/anacron/cron.daily 文件的时间戳

    CentOS6使用/etc/cron.daily/tmpwatch定时清除临时文件
    CentOS7使用systemd-tmpfiles-setup服务实现

    crontab:用户cron任务
    /etc/crontab:执行日志
    /etc/crontab :系统cron任务:系统维护作业

    1. [root@newhostname aaa]# cat /etc/crontab
    2. SHELL=/bin/bash
    3. PATH=/sbin:/bin:/usr/sbin:/usr/bin
    4. MAILTO=root
    5. # For details see man 4 crontabs
    6. # Example of job definition:
    7. # .---------------- minute (0 - 59)
    8. # | .------------- hour (0 - 23)
    9. # | | .---------- day of month (1 - 31)
    10. # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
    11. # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    12. # | | | | |
    13. # * * * * * user-name command to be executed
    14. 分 时 日 月 周

    在最后一行添加要定时的任务

    时间格式

    @reboot Run once after reboot.
    @yearly 0 0 1 1 *
    @annually 0 0 1 1 *
    @monthly 0 0 1 * *
    @weekly 0 0 * * 0
    @daily 0 0 * * *
    @hourly 0 * * * *

    crontab命令定义,每个用户都有专用的cron任务文件:/var/spool/cron/USERNAME

    crontab命令:

    crontab [-u user] [-l | -r | -e] [-i]
    -l: 列出所有任务;
    -e: 编辑任务;
    -r: 移除所有任务;
    -i:同-r一同使用,以交互式模式移除指定任务
    -u user: 仅root可运行,指定用户管理cron任务

    控制用户执行计划任务
    /etc/cron.{allow,deny}  #连个配置文件,和at 。{allow,deny}规则一样

    在使用cron定时巡检时如果有问题的监控项可以通过过 wall (– send a message to everybody’s terminal.)来对用户发送广播。

  • 相关阅读:
    Appium简介
    本章小结
    测试角色定位,岗位职责
    如何做好空降管理者
    如何把控产品质量
    Appium自动化测试教程-自学网-monkeyrunner API
    Appium自动化测试教程-自学网-monkeyrunner简介
    Appium自动化测试教程-自学网-monkey日志管理
    Appium自动化测试教程-自学网-monkey自定义脚本实践
    Appium自动化测试教程-自学网-monkey参数
  • 原文地址:https://www.cnblogs.com/momenglin/p/8485964.html
Copyright © 2011-2022 走看看