Linux 系统定时任务:crontab,anacron

一、Cron 服务

1. 启动服务

  1. service cron start

2. 关闭服务

  1. service cron stop

3. 重启服务

  1. service cron restart

4. 重新载入配置

  1. service cron reload

5. 查看服务状态

  1. service cron status

二、用户定时任务

1. 选项

-e:执行文字编辑器来设定定时任务
-l:列出目前所有定时任务
-r:删除目前所有定时任务(慎用)

要经常备份定时任务。因为键盘上 r 和 e 是挨着的,很可能会按错导致删除所有定时任务。

2. crontab 格式

分 时 日 月 周 command

代表意义 command
数字范围 0-59 0-23 1-31 1-12 0-7(0和7都表示周日) 需要执行的命令
特殊字符 代表意义
* 代表任何时刻。比如第一个 * 代表一个小时中每一分钟都执行一次。
, 代表不连续的时间。比如 0 8,12,16 * * * command 表示每天8点,12点,16点执行一次
- 代表连续时间范围。比如 0 8-12 * * * command 表示每天8点到12点,每小时都执行一次
*/n 那个 n 代表数字,代表‘每隔 n 单位时间执行一次’。例如 */5 * * * * command 表示每五分钟进行一次

注意事项

  1. 当  和  都不为 * 时,任意一个满足条件,都会运行定时任务。、
  2. */n 并不是真正意义上的 ‘每隔 n 单位时间执行一次’,详情:https://segmentfault.com/q/10...

3. 实例

  • 每1分钟执行一次command
  1. * * * * * command
  • 每小时的第3和第15分钟执行
  1. 3,15 * * * * command
  • 在上午8点到11点的第3和第15分钟执行
  1. 3,15 8-11 * * * command
  • 每隔两天的上午8点到11点的第3和第15分钟执行
  1. 3,15 8-11 */2 * * command
  • 每个星期一的上午8点到11点的第3和第15分钟执行
  1. 3,15 8-11 * * 1 command
  • 在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup
  1. 0 6-12/3 * 12 * /usr/bin/backup
  • 周一到周五每天下午 5:00 寄一封信给 alex@domain.name
  1. 0 17 * * 1-5 mail -"hi" alex@domain.name < /tmp/maildata
  • 每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"
  1. 20 0-23/2 * * * echo "haha"
  • 每两个小时重启一次apache
  1. 0 */2 * * * /sbin/service httpd restart
  • 每天7:50开启ssh服务
  1. 50 7 * * * /sbin/service sshd start
  • 每天22:50关闭ssh服务
  1. 50 22 * * * /sbin/service sshd stop
  • 每月1号和15号检查/home 磁盘
  1. 0 0 1,15 * * fsck /home
  • 每小时的第一分执行 /home/bruce/backup这个文件
  1. 1 * * * * /home/bruce/backup
  • 每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。
  1. 0 3 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;
  • 每月的1、11、21、31日是的6:30执行一次ls命令
  1. 30 6 */10 * * ls

三、系统定时任务

1. /etc/crontab 配置文件

crintab -e 是编辑当前用户执行的命令,也就是不同的用户身份可以执行自己的定时任务。可是有些定时任务需要系统执行,这时我们就需要编辑 /etc/crontab 这个配置文件了。

/etc/crontab 文件内容(ubuntu14.04)

  1. # /etc/crontab: system-wide crontab
  2. # Unlike any other crontab you don't have to run the `crontab'
  3. # command to install the new version when you edit this file
  4. # and files in /etc/cron.d. These files also have username fields,
  5. # that none of the other crontabs do.
  6. # 一些环境变量
  7. SHELL=/bin/sh
  8. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  9. # 默认的任务,定期执行
  10. # /etc/cron.hourly
  11. # /etc/cron.daily
  12. # /etc/cron.weekly
  13. # /etc/cron.monthly
  14. # 文件夹下的脚本文件。
  15. #
  16. # m h dom mon dow user command
  17. 17 * * * * root cd / && run-parts --report /etc/cron.hourly
  18. 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  19. 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  20. 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
  21. #
  1. 只需要将定时任务按照格式添加到 `/etc/crontab` 文件中,`service cron reload` 重新载入配置即可。

2. /etc/cron.d 目录

  1. 将定时任务添加到 `/etc/cron.d` 目录下,按照 `m h dom mon dow user command` 的格式编写定时任务,`service cron reload` 重新载入配置即可。
  1. [vagrant/etc/cron.d] ]$ll
  2. total 8K
  3. -rw-r--r-- 1 root root 712 Feb 5 12:44 php
  4. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
  5. [vagrant/etc/cron.d] ]$cat php
  6. # /etc/cron.d/php@PHP_VERSION@: crontab fragment for PHP
  7. # This purges session files in session.save_path older than X,
  8. # where X is defined in seconds as the largest value of
  9. # session.gc_maxlifetime from all your SAPI php.ini files
  10. # or 24 minutes if not defined. The script triggers only
  11. # when session.save_handler=files.
  12. #
  13. # WARNING: The scripts tries hard to honour all relevant
  14. # session PHP options, but if you do something unusual
  15. # you have to disable this script and take care of your
  16. # sessions yourself.
  17. # 每隔30分钟寻找并清除旧的会话
  18. # Look for and purge old sessions every 30 minutes
  19. 09,39 * * * * root [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi

3. /etc/cron.{hourly,daily,weekly,monthly}

查看 /etc/cron.{hourly,daily,weekly,monthly} 这四个目录及目录下的文件

  1. [vagrant/etc] ]$ll -d cron.{hourly,daily,weekly,monthly}
  2. drwxr-xr-x 2 root root 4.0K Mar 29 09:21 cron.daily/
  3. drwxr-xr-x 2 root root 4.0K May 10 01:52 cron.hourly/
  4. drwxr-xr-x 2 root root 4.0K Jul 21 2015 cron.monthly/
  5. drwxr-xr-x 2 root root 4.0K Jul 21 2015 cron.weekly/
  6. [vagrant/etc] ]$ll cron.{hourly,daily,weekly,monthly}
  7. cron.daily:
  8. total 60K
  9. -rwxr-xr-x 1 root root 625 Sep 18 2017 apache2*
  10. -rwxr-xr-x 1 root root 16K Apr 10 2014 apt*
  11. -rwxr-xr-x 1 root root 314 Feb 18 2014 aptitude*
  12. -rwxr-xr-x 1 root root 355 Jun 4 2013 bsdmainutils*
  13. -rwxr-xr-x 1 root root 256 Mar 7 2014 dpkg*
  14. -rwxr-xr-x 1 root root 372 Jan 22 2014 logrotate*
  15. -rwxr-xr-x 1 root root 1.3K Apr 10 2014 man-db*
  16. -rwxr-xr-x 1 root root 435 Jun 20 2013 mlocate*
  17. -rwxr-xr-x 1 root root 249 Feb 17 2014 passwd*
  18. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
  19. -rwxr-xr-x 1 root root 2.4K May 13 2013 popularity-contest*
  20. -rwxr-xr-x 1 root root 322 Apr 11 2014 upstart*
  21. cron.hourly:
  22. total 8.0K
  23. -rwxr-xr-x 1 root root 43 May 10 01:52 date*
  24. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
  25. cron.monthly:
  26. total 4.0K
  27. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
  28. cron.weekly:
  29. total 16K
  30. -rwxr-xr-x 1 root root 730 Feb 23 2014 apt-xapian-index*
  31. -rwxr-xr-x 1 root root 427 Apr 16 2014 fstrim*
  32. -rwxr-xr-x 1 root root 771 Apr 10 2014 man-db*
  33. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
  1. 由此可见,`/etc/cron.{hourly,daily,weekly,monthly}` 这四个目录中都是一些有执行权限的脚本文件。

cron.hourly 下的脚本每小时执行一次
cron.daily 下的脚本每天执行一次
cron.weekly 下的脚本每周执行一次
cron.monthly 下的脚本每月执行一次

四、anacron

1. anacron 是什么

  1. anacron 是用来保证在系统关机时错过的定时任务可以在系统开机之后在执行

2. anacron 的监测周期

  • anacron 会使用一天、七天、一个月作为监测周期。
  • 在系统的 /var/spool/anacron/ 目录中存在 cron.{daily,weekly,monthly} 文件,用于记录上次执行 cron 的时间。
  • 将记录的时间与当前时间作比较,若两个时间差超过了 anacron 的指定时间差值,证明有cron任务被漏执行。

3. /etc/anacrontab 文件

  1. [vagrant/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=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  6. HOME=/root
  7. LOGNAME=root
  8. # These replace cron's entries
  9. 1 5 cron.daily run-parts --report /etc/cron.daily
  10. 7 10 cron.weekly run-parts --report /etc/cron.weekly
  11. @monthly 15 cron.monthly run-parts --report /etc/cron.monthly

对比 /etc/crontab

返回查看 /etc/crontab 文件中有这样一段:

  1. 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  2. 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  3. 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
  • test -x

    test -x /usr/sbin/anacron 表示 “检测文件 /usr/sbin/anacron 是否存在且具有“可执行”权限”

    若 test -x /usr/sbin/anacron 为 true,则不执行 || 后面的命令,若为 false,则执行 || 后面的命令。

    即,判断系统是否安装了 anacron , 若安装了,则忽略 /etc/crontab 中的这三条定时任务,改为使用 /etc/anacrontab 中的配置。

  • run-parts --report

    遍历目标文件夹,执行第一层目录下具有可执行权限的文件。

/etc/anacrontab 计划任务格式

天数 延迟时间(分) 工作名称 实际执行的命令
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthly

以 cron.daily 为例说明 anacron 执行过程

  1. 从 /etc/anacrontab 分析到 cron.daily 的天数为 1 天。
  2. 从 /var/spool/anacron/cron.daily 中获取最后一次执行 anacron 的时间。
  3. 将上面获取的时间与当前时间做对比,若相差时间为1天以上(含一天),就准备执行 cron.daily
  4. 根据 /etc/anacrontab 的设置,将延迟5分钟执行 cron.daily。
  5. 5分钟后,开始执行 run-parts --report /etc/cron.daily 命令,即执行 /etc/cron.daily目录下的所有可执行文件。
赞  |   3收藏  |  8
赞赏支持
如果觉得我的文章对你有用,请随意赞赏

你可能感兴趣的

 

Nick · 2018年05月11日

ubuntu 系统?

 

 赞 回复

是啊

 
— 白菜1031作者 · 2018年05月11日
 

Nick · 2018年05月11日

run-parts --report /etc/cron.daily 改成 run-parts --report 自定义目录 能成功?

 

 赞 回复

亲测可以成功。

  1. [vagrant/tmp] ]$ll
  2. total 4.0K
  3. drwxr-xr-x 2 root root 4.0K May 11 04:13 cron.test/
  4. [vagrant/tmp] ]$ll cron.test/
  5. total 8.0K
  6. -rwxr-xr-x 1 root root 36 May 11 04:13 date*
  7. -rwxr-xr-x 1 root root 37 May 11 04:13 date2*
  8. [vagrant/tmp] ]$sudo run-parts --report cron.test
  9. [vagrant/tmp] ]$ll
  10. total 12K
  11. drwxr-xr-x 2 root root 4.0K May 11 04:13 cron.test/
  12. -rw-r--r-- 1 root root 29 May 11 04:14 date2.log
  13. -rw-r--r-- 1 root root 29 May 11 04:14 date.log
  14. [vagrant/tmp] ]$cat cron.test/date
  15. #! /bin/bash
  16. date >> /tmp/date.log
  17. [vagrant/tmp] ]$cat cron.test/date2
  18. #! /bin/bash
  19. date >> /tmp/date2.log
 
— 白菜1031作者 · 2018年05月11日

/1 * run-parts /home/nick/sh/ #每分钟执行 /home/nick/sh/ 目录内的脚本,CentOS 系统生效,Ubuntu 系统不生效,原因暂时未知。

 
— Nick· 2018年05月11日

/1 * root run-parts /home/nick/sh/ 这样两种系统都不成功,日志也不报错

 
— Nick· 2018年05月11日
 

丧我 · 2018年05月12日

现在 systemd.timer 也基本普及了吧?

Linux 系统定时任务:crontab,anacron的更多相关文章

  1. Linux系统定时任务crond那些事

    1 Linux系统定时任务 1.1 定时任务介绍 1.1.1 Crond是什么? Crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件.Centos5/ linux系统安装完操作 ...

  2. Linux学习之九-Linux系统定时任务

    Linux系统定时任务 在一些实际工作中需要机器在某个时间自动执行某个任务,不需要人为在此时刻参与,可以建立一个定时任务. crond 服务是linux下用来周期性的执行某种任务或等待处理某些事件的一 ...

  3. 『学了就忘』Linux系统定时任务 — 89、任务调度工具anacron

    目录 1.任务调度工具anacron介绍 2.新旧版本Linux中anacron工具的区别 3./etc/cron.{daily,weekly,monthly}目录说明 4.anacron命令 5./ ...

  4. 『学了就忘』Linux系统定时任务 — 87、只执行一次的定时任务

    目录 1.at服务管理 2.at命令的访问控制 3.at命令 4.其他at管理命令 5.总结 定时任务是在服务器上常用到的一个工作. 在你指定的时间,系统会自动执行你指定的程序(脚本或者命令). Li ...

  5. 『学了就忘』Linux系统定时任务 — 88、循环执行定时任务

    目录 1.crond服务管理与访问控制 2.crontab命令的访问控制 3.用户级别的crontab命令 4.crontab命令的注意事项 5.系统的crontab设置 (1)/etc/cronta ...

  6. linux系统定时任务crond入门

    1,Crond: Crond是linux系统中用来定期执行命令或指定程序任务的一种服务或者软件.(Centos5以后默认存在) 当优化开机自启动的时候,第一个就是crond. Crond服务默认情况( ...

  7. Linux系统定时任务介绍

    定时任务Crond介绍 1)crond是什么? 守护进程:持续运行的程序,不退出的进程. 为什么要使用crond定时任务呢? 1)Linux下定时任务的种类 at crontab  anacron 2 ...

  8. PHP面试系列 之Linux(二)---- Linux系统定时任务

    环境:ubuntu 16 一.cron实现定时任务 cron实现的定时任务是周期性循环执行的. 1.安装cron sudo apt-get install cron 2.添加定时任务(进行编辑) cr ...

  9. linux系统下crontab 配置启动定时任务

    1 crontab -e 配置启动定时任务 */1 * * * * sh /home/admin/application/wd/core-python/getMemPositionFromAnaual ...

随机推荐

  1. CrashLoopBackOff的解决办法之一

    问题来源 # kubectl get pods -n assembly NAME READY STATUS RESTARTS AGE alertmanager-858b7749c5-6jsfh 0/1 ...

  2. Java单例模式实现,一次性学完整,面试加分项

    单例模式是设计模式中使用最为普遍的一种模式.属于对象创建模式,它可以确保系统中一个类只产生一个实例.这样的行为能带来两大好处: 对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而 ...

  3. Java集合--Java核心面试知识整理(二)

    目前CSDN,博客园,简书同步发表中,更多精彩欢迎访问我的gitee pages 目录 JAVA集合 2.1 接口继承关系和实现 2.2 List 2.2.1 ArrayList(数组) 2.2.2 ...

  4. Node.js/Vue.js使用jsSHA库进行SHA1/2/3加密

    1 概述 jsSHA是一个用JS+TS实现完整SHA系列加密算法的加密库,包括: SHA1 SHA-224/256/384/512 SHA3-224/256/384/512 SHAKE128/256 ...

  5. 【pytest官方文档】解读fixtures - 3. fixtures调用别的fixtures、以及fixture的复用性

    pytest最大的优点之一就是它非常灵活. 它可以将复杂的测试需求简化为更简单和有组织的函数,然后这些函数可以根据自身的需求去依赖别的函数. fixtures可以调用别的fixtures正是灵活性的体 ...

  6. Vue3发布半年我不学,摸鱼爽歪歪,哎~就是玩儿

    是从 Vue 2 开始学基础还是直接学 Vue 3 ?尤雨溪给出的答案是:"直接学 Vue 3 就行了,基础概念是一模一样的." 以上内容源引自最新一期的<程序员>期刊 ...

  7. 前端实用程序包utils - 开发工作流(一)

    写在前面 早年间有幸在Raychee哥门下当小弟,学到两把刷子.在编程路上,他的很多思想深深影响了我,比如笔者今天要分享的主题.在程序开发中,有个utils包,叫做实用程序包,程序员们会把项目中通用的 ...

  8. 1061 Dating

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  9. Linux 究级基础入门命令整理

    Linux 究级基础入门命令整理 条条框框,三三两两,怎讷个这么多,哈哈!no zuo no die. 纯粹个人菜鸟笔记,望大神笑纳! 后续,未完!! 查看系统信息 uname -a - 查看内核/操 ...

  10. 千位分隔符,音频音量加强,transform 垂直居中

    1.最近做阴阳师日本官网,其中有个功能是获取预约人数,设计稿上的人数是这样151,567,000,想了想自己写还有点麻烦,于是网上copy了代码,再修改了下. 其中,有一点需要注意的是:函数中的str ...