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

    一.cron介绍
    linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题。
    1. var/spool/cron/

    目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名
    2. _/etc/crontab

    这个文件负责调度各种管理和维护任务。
    3. /etc/cron.d/

    这个目录用来存放任何要执行的crontab文件或脚本。
    我们还可以把脚本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录中,让它每小时/天/星期、月执行一次。

    二.crontab的使用
    我们常用的命令如下:
    crontab [-u username]    //省略用户表表示操作当前用户的crontab
    1. -e (编辑工作表)
    2. -l (列出工作表里的命令)
    3. -r (删除工作作)
    我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。
    crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有
    * 取值范围内的所有数字
    / 每过多少个数字
    - 从X到Z
    ,散列数字

    三.常见定时任务设置
    实例1:每1分钟执行一次myCommand

    * * * * * myCommand

    实例2:每小时的第3和第15分钟执行

    3,15 * * * * myCommand

    实例3:在上午8点到11点的第3和第15分钟执行

    3,15 8-11 * * * myCommand

    实例4:每隔两天的上午8点到11点的第3和第15分钟执行

    3,15 8-11 */2 * * myCommand

    实例5:每周一上午8点到11点的第3和第15分钟执行

    3,15 8-11 * * 1 myCommand

    实例6:每晚的21:30重启smb

    30 21 * * * /etc/init.d/smb restart

    实例7:每月1、10、22日的4 : 45重启smb

    45 4 1,10,22 * * /etc/init.d/smb restart

    实例8:每周六、周日的1 : 10重启smb

    10 1 * * 6,0 /etc/init.d/smb restart

    实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb

    0,30 18-23 * * * /etc/init.d/smb restart

    实例10:每星期六的晚上11 : 00 pm重启smb

    0 23 * * 6 /etc/init.d/smb restart

    实例11:每一小时重启smb

    0 */1 * * * /etc/init.d/smb restart

    实例12:晚上11点到早上7点之间,每隔一小时重启smb

    0 23-7/1 * * * /etc/init.d/smb restart

     

    四.实例操作

    1.文件实时写入:

    1) 查看定时任务状态

    [root@localhost ~]#service crond status
    Redirecting to /bin/systemctl status crond.service
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2021-09-20 01:22:18 CST; 24s ago
     Main PID: 17516 (crond)
       CGroup: /system.slice/crond.service
               └─17516 /usr/sbin/crond -n

    2) 关闭/开启定时任务

    [root@localhost ~]# service crond stop
    Redirecting to /bin/systemctl stop crond.service
    [root@localhost ~]# service crond start
    Redirecting to /bin/systemctl start crond.service

    3) 编辑定时任务

    [root@localhost ~]# crontab -e
    * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt #每分钟执行一次

    4) 查看已存在的定时任务

    [root@localhost ~]# crontab -l
    * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt

    5) 验证校验生成

    [root@localhost ~]# cat 123.txt 
    2021-09-20 01:13:01
    2021-09-20 01:14:01
    2021-09-20 01:15:01

    2. 定期清理对应目录下的文件

    1) 预制数据:

    [root@localhost test20210920]# echo abcdefg | tee -a file{1..5}.log
    abcdefg
    [root@localhost test20210920]# du -sh *
    4.0K    file1.log
    4.0K    file2.log
    4.0K    file3.log
    4.0K    file4.log
    4.0K    file5.log

    2) 添加定时任务

    [root@localhost ~]# crontab -e
    
    * * * * * find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} ;

    3) 检查定时任务执行,1分钟左右

    [root@localhost test20210920]# du -sh *
    0    file1.log
    0    file2.log
    0    file3.log
    0    file4.log
    0    file5.log

    4) 查看定时任务执行情况:

    [root@localhost test20210920]# tail -5 /var/log/cron
    Sep 20 01:56:33 localhost crontab[17797]: (root) END EDIT (root)
    Sep 20 01:56:51 localhost crontab[17802]: (root) BEGIN EDIT (root)
    Sep 20 01:56:54 localhost crontab[17802]: (root) END EDIT (root)
    Sep 20 01:57:01 localhost CROND[17809]: (root) CMD (find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} ;)
    Sep 20 01:58:01 localhost CROND[17819]: (root) CMD (find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} ;)

    五.常见错误

    1.errors in crontab file, can't install

     解决方式:

    因为你的crontab格式错误,即没有按照规则写

    查看原命令并修正:

    echo `date +"%Y-%m-%d %H:%M"` >> 123.txt

    修改后正常保存:

    * * * * * echo `date +"%Y-%m-%d %H:%M"` >> 123.txt

     2.接以上错误:

    邮件内报错:cat /var/spool/mail/root

    解决方式: 

    crontab内%需要转义,修复定时任务正常

    * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt
  • 相关阅读:
    hihocoder [Offer收割]编程练习赛14 投掷硬币
    hihocoder [Offer收割]编程练习赛14 小Hi和小Ho的礼物
    CodeForces
    [HNOI2004] 打砖块
    CodeForces
    hdu4028 The time of a day[map优化dp]
    hdu5009 Paint Pearls[指针优化dp]
    hdu4719 Oh My Holy FFF[线段树优化dp]
    hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]
    hdu3709 Balanced Number[数位dp]
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15313228.html
Copyright © 2011-2022 走看看