美文网首页
linux系列15-计划任务cron

linux系列15-计划任务cron

作者: 蕴重Liu | 来源:发表于2019-07-13 15:27 被阅读0次

    调度执行
    作用:周期性任务,主要用途是定期备份数据

    Schedule ont-time tasks with at --一次性调度执行 at
    Schedule recurring jobs with cron --循环调度执行cron
    Schedule recurring system jobs
    所有计划任务执行中的输出都会以邮件的方式发送给指定用户,除非重定向

    一次性调度执行 at

    语法格式:
    at <TIMESPEC>
    now +5min
    teatime tomorrow(teatime is 16:00)
    noon +4 days
    5pm august 3 2018

    --未用root登录,导致执行失败
    lyq@DESKTOP-40030BI:~$ vim jack.at
    lyq@DESKTOP-40030BI:~$ cat jack.at
    useradd u200
    useradd u300
    touch /home/'date +%F'_jack.txt
    
    lyq@DESKTOP-40030BI:~$ at now +1min < jack.at
    warning: commands will be executed using /bin/sh
    job 2 at Sat Jul 13 14:42:00 2019
    Can't open /var/run/atd.pid to signal atd. No atd running?
    lyq@DESKTOP-40030BI:~$ id u200
    id: ‘u200’: no such user
    lyq@DESKTOP-40030BI:~$ date
    Sat Jul 13 14:42:48 DST 2019
    lyq@DESKTOP-40030BI:~$ id u777
    id: ‘u777’: no such user
    lyq@DESKTOP-40030BI:~$ id u300
    id: ‘u300’: no such user
    

    注意:sudo执行需要考虑tty问题

    lyq@DESKTOP-40030BI:~$ visudo
    visudo: /etc/sudoers: Permission denied
    lyq@DESKTOP-40030BI:~$ sudo visudo
    visudo: /etc/sudoers.tmp unchanged
    

    crond

    systemctl status crond.service
    pa aux |grep crond
    

    crond进程每分钟会处理一次计划任务

    crontab -e
    

    语法格式
    Minutes Hours Day-of-Month Month Day-of-Week Command

    * * * * * command
    Minutes  -- 0-59
    Hours  --  0-23
    Day-of-Month  -- 1-31
    Month  -- 1-12  or jan feb mar apr
    Day-of-Week  --  0-6  or Sunday=0 or 7
    
    用户级
    --存储路径
    (base) root@dell:~ # ls /var/spool/cron
    atjobs  atspool  crontabs
    
    0 2 * * * /mysql_back.sh      --每天2点整
    0 2 1 * * /mysql_back.sh      --每月1号2点整
    0 2 14 2 * /mysql_back.sh     --每月2月14号2点整
    0 2 * * 7 /mysql_back.sh       --每周日2点整
    0 2 * 6 5 /mysql_back.sh     --每年6月的周五2点整   
    0 2 14 * 7 /mysql_back.sh      --每月14号2点整  或 每周日2点整   ,两个时间都执行
    0 2 14 2 7 /mysql_back.sh     --每月2月14号2点整  或  每周日2点整   ,两个时间都执行
    0 2 2 * 5 /mysql_back.sh    --每周五2点
    
    */5 * * * * /mysql_back.sh       --每隔5分钟
    0 2 1,4,6 * * /mysql_back.sh      --每月1/4/6号的2点运行
    0 2 5-9 * * /mysql_back.sh        --每月5-9号的2点运行
    0 * * * * /mysql_back.sh         --
    * * 2 * * /mysql_back.sh        --2号那天,每分钟都执行,逻辑有错
    * 2 * * * /mysql_back.sh        --每天2点整的每一个分钟,逻辑有错
    
    系统级

    主要用途:
    临时文件的清理 /tmp/var/tmp
    系统信息的采集 sar
    日志的轮转(切割)logrotate

    --定义位置1
    (base) root@dell:~ # cat /etc/crontab
    # /etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the `crontab'
    # command to install the new version when you edit this file
    # and files in /etc/cron.d. These files also have username fields,
    # that none of the other crontabs do.
    
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    # m h dom mon dow user  command
    17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
    25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    #
    
    --定义位置2: /etc/cron.d/*
    (base) root@dell:~ # ls /etc/cron.d
    mdadm  popularity-contest
    (base) root@dell:~ # cat /etc/cron.d/mdadm
    #
    # cron.d/mdadm -- schedules periodic redundancy checks of MD devices
    #
    # Copyright © martin f. krafft <madduck@madduck.net>
    # distributed under the terms of the Artistic Licence 2.0
    #
    
    # By default, run at 00:57 on every Sunday, but do nothing unless the day of
    # the month is less than or equal to 7. Thus, only run on the first Sunday of
    # each month. crontab(5) sucks, unfortunately, in this regard; therefore this
    # hack (see #380425).
    57 0 * * 0 root if [ -x /usr/share/mdadm/checkarray ] && [ $(date +\%d) -le 7 ]; then /usr/share/mdadm/checkarray --cron --all --idle --quiet; fi
    

    crond仅仅会执行每小时定义的脚本 /etc/cron.hourly

    crond 实际是启动anacrond进程
    每小时的01分钟,crond进程会唤醒anacrond进程,防止某些任务被错过 /etc/anacrontab

    相关文章

      网友评论

          本文标题:linux系列15-计划任务cron

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