美文网首页
Linux定时任务

Linux定时任务

作者: 其实我很dou | 来源:发表于2018-01-04 21:54 被阅读0次

Linux定时任务

一. at一次性定时任务

  • 查看at是否安装
    chkconfig --list | grep atd

  • 启动at服务
    service atd start

  • at的访问控制

    • /etc/at.allow(白名单)
    • /etc/at.deny(黑名单, 对root不起作用)
    • 如果两个文件都不存在, 则只有root能使用
  • at[选项]时间

    • 选项一般不常用
    • 时间:
      • HH:MM 02:30
      • HH::MM YYYY-MM-DD 20:30 20180112
      • HH::MM[am|pm][mouth][day] 02:30pm July 25
      • HH::MM[am|pm] + [minutes|hours|days|weeks] now + 5minutes
  • 示例

at now + 2minutes
# 2min中后执行hello.sh脚本

[root@localhost ~]# at now + 2minutes
at> /root/hello.sh
# 按ctrl+d退出保存

at> <EOT>
job 3 at 2018-01-02 22:11

# 查看at
[root@localhost ~]# atq
3   2018-01-02 22:11 a root

# at -c 工作号
[root@localhost ~]# at -c 5

二. 循环定时任务crontab -e

  • 检查定时任务是否开启
    service crond status
    chkconfig crond on

  • 访问控制
    crontab是绑定当前登陆用户, 每个用户都有自己的crontab

    • 当系统中有/etc/cron.allow文件时, 只有写入到该文件的用户可以使用crontab命令
    • 当系统中有/etc/cron.deny文件时, 写入到该文件的用户不可以使用crontab命令, 黑名单
    • /etc/cron.allow优先级高于/etc/cron.deny
  • crontab[选项]

    • -e: 编辑crontab定时任务
    • -l: 查询crontab定时任务
    • -r: 删除当前用户所有的crontab任务
  • crontab格式
    * * * * * 命令

项目 含义 范围
第一个* 一小时当中第几分钟 0-59
第二个* 一天当中第几小时 0-23
第三个* 一个月当中的第几天 1-31
第四个* 一年当中的第几个月 1-12
第五个* 一周当中的星期几 0-7(0和7都是星期日)
项目 含义
* 代表任意时间
, 代表不连续时间, 如0 8,12,16 * * * 命令 表示在8点0分, 12点0分, 16点0分 都执行一次命令
- 连续时间范围, 如 0 5 * * 1-6 命令 表示周一到周五凌晨五点0分执行命令
*/n 每隔多久执行一次, 如 * */5 * 3 * 命令 表示三月份每天每隔5小时执行一次命令
  • crontab注意事项
    • 六个选项都不能为空, 必须填写, 如果不确定, 使用"*"代表任意时间
    • crontab定时任务, 最小有效时间是分钟, 最大是月份, 年和秒不支持
    • 在定义时间时, 日期和星期最好不要在一条定时任务中出现, 都是以天为单位的, 容易引起混乱, 如果出现, 比如 0 5 1,2 * 2 命令表示每个月1号, 二号或者星期2的凌晨五点执行命令

三. 系统定时任务

系统定时任务, 用于执行一些和用户不想关的系统定时任务

  • 编辑/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
# 需要定义用户
* 5 * * * root /root/test/hello.sh
  • anacron配置和总结
    anacron是用来保证在系统关机的时候错过的定时任务, 可以在系统开机之后再执行,
    我们可以需要定时执行的脚本拷贝到一下三个目录中, 就可以按照各自目录的时间点随机时间执行
/etc/cron.daily #该目录下脚本每天执行一次
/etc/cron.weekly # 该目录下脚本每周执行一次(和上次执行时间比较 > 7 day)
/etc/cron.monthly # 该目录下脚本每月执行一次(和上次执行时间比较 > 1 months)
  • anacron主配置文件如下:
    vi /etc/anacron
...
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
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

  • anacron工作流程大概为(以/etc/cron.daily为例):

    • 开机后检测/var/spool/anacron/cron.daily值, 和当前时间作比较, 如果大于一天, 就会准备执行 run-parts /etc/cron.daily下的脚本
    • 系统会强制延后五分钟, 再0-45分钟之后执行, 并且只能在3点-22点执行
    • nice会设置优先级
    • run-part会0-45分钟随机时间运行/etc/cron.daily目录下的脚本
  • vi /etc/cron.d/0hourly

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 每隔一分钟运行/etc/cron.hourly下的脚本
01 * * * * root run-parts /etc/cron.hourly
  • vi /etc/cron.hourly/0anacron
#!/bin/bash
# Skip excecution unless the date has changed from the previous run 
# 检测上次执行时间
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power &> /dev/null
    if test $? -eq 1; then
    exit 0
    fi
fi
// 设置依赖任务, 此处并不能看懂
/usr/sbin/anacron -s

相关文章

  • day 17

    第13章 Linux系统定时任务Cron(d)服务应用实践 1.1、Linux定时任务 1.1.1、什么是定时任务...

  • linux crontab: 定时任务

    参考 crontab 定时任务 Linux之crontab定时任务

  • linux Crontab

    Linux Crontab:Linux中用于执行定时任务的工具crontab -e:编辑定时任务crontab -...

  • 定时任务

    Linux定时任务 什么是定时任务: 周期性的执行任务计划的软件,linux里定时任务常用软件有:Crond 使用...

  • day17

    Linux系统定时任务 3W1H 框架 Linux系统定时任务: 1、什么是定时任务? 周期性的执行任务计划的软件...

  • day17-Linux系统定时任务

    Linux系统定时任务 1.什么是定时任务? 周期性的执行任务计划的软件,Linux定时任务软件的常用软件cron...

  • 作业-第04周--课堂-Day17-linux系统定时任务Cro

    Day17 课堂笔记 1 Linux系统定时任务 1、什么是定时任务?周期性的执行任务计划的软件,Linux定时任...

  • Linux笔记

    Linux定时任务 Mysql备份 Linux+Mysql定时备份 * * * * * user task分 时 ...

  • 79.linux定时任务

    Linux定时任务 通过制定 ```crontab -e``编辑和开启定时任务

  • Linux定时任务

    Linux定时任务 centOS: 使用crontab -e //编辑 crontab -l //查看定时任务 生...

网友评论

      本文标题:Linux定时任务

      本文链接:https://www.haomeiwen.com/subject/zgmdnxtx.html