什么是crontab?

crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。该词来源于希腊语 chronos(χρνο),原意是时间。通常,crontab储存的指令被守护进程激活, crond常常在后台运行,每一分钟检查是否有预定的作业需要执行。这类作业一般称为cron jobs。

怎么使用?

1. 安装

如果没有安装,就进行安装。

➜  Downloads rpm -qa | grep cron
crontabs-1.11-16.20150630git.fc28.noarch
cronie-1.5.1-9.fc28.x86_64
cronie-anacron-1.5.1-9.fc28.x86_64 

2. 服务启动和关闭

使用 systemctl 管理

➜  Downloads systemctl restart crond.service

3. 配置文件

➜  ~ ls -l /etc/cron*
-rw-r--r--. 1 root root 0 Feb 7 13:58 /etc/cron.deny
-rw-r--r--. 1 root root 451 Feb 7 13:57 /etc/crontab /etc/cron.d:
total 8
-rw-r--r--. 1 root root 128 Feb 7 13:58 0hourly
-rw-r--r--. 1 root root 108 Aug 4 2017 raid-check /etc/cron.daily:
total 24
-rwxr-xr-x. 1 root root 18812 Apr 26 10:21 google-chrome
-rwxr-xr-x. 1 root root 189 Jan 4 19:58 logrotate /etc/cron.hourly:
total 4
-rwxr-xr-x. 1 root root 575 Feb 7 13:58 0anacron /etc/cron.monthly:
total 0 /etc/cron.weekly:
total 4
-rwxr-xr-x. 1 root root 603 Aug 5 2017 98-zfs-fuse-scrub
  • cron.daily是每天执行一次的任务
  • cron.weekly是每个星期执行一次的任务
  • cron.monthly是每月执行一次的任务
  • cron.hourly是每个小时执行一次的任务
  • cron.d是系统自动定期需要做的任务
  • crontab是设定定时任务执行文件
  • cron.deny文件就是用于控制不让哪些用户使用crontab的功能

每个用户都有自己的cron配置文件,通过crontab -e 就可以编辑,一般情况下我们编辑好用户的cron配置文件保存退出后,系统会自动就存放于/var/spool/cron/目录中,文件以用户名命名.linux的cron服务是每隔一分钟去读取一次/var/spool/cron,/etc/crontab,/etc/cron.d下面所有的内容.

➜  ~ sudo ls -l /var/spool/cron /etc/crontab /etc/cron.d
[sudo] password for xuyaowen:
-rw-r--r--. 1 root root 451 Feb 7 13:57 /etc/crontab /etc/cron.d:
total 8
-rw-r--r--. 1 root root 128 Feb 7 13:58 0hourly
-rw-r--r--. 1 root root 108 Aug 4 2017 raid-check /var/spool/cron:
total 0

4. 配置文件格式

*           *        *        *        *           command

minute   hour    day   month   week      command

分          时         天      月        星期       命令

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”。

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”。

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

5. crontab 命令使用方法

可以通过 man 命令查看具体的使用的方法

 crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
crontab -n [ hostname ]
crontab -c

-u  Appends the name of the user whose crontab is to be modified. If this option is not used, crontab examines "your" crontab, i.e., the crontab of the
person executing the command. Note that su(8) may confuse crontab, thus, when executing commands under su(8) you should always use the -u option.
If no crontab exists for a particular user, it is created for him the first time the crontab -u command is used under his username.

-l  Displays the current crontab on standard output.

-r  Removes the current crontab.

-e  Edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified
crontab will be installed automatically.

-i  This option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.

-s  Appends the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs - see the docu‐
mentation of MLS_LEVEL in crontab(5).

-n  This option is relevant only if cron(8) was started with the -c option, to enable clustering support. It is used to set the host in the cluster
which should run the jobs specified in the crontab files in the /var/spool/cron directory. If a hostname is supplied, the host whose hostname
returned by gethostname(2) matches the supplied hostname, will be selected to run the selected cron jobs subsequently. If there is no host in the
cluster matching the supplied hostname, or you explicitly specify an empty hostname, then the selected jobs will not be run at all. If the hostname
is omitted, the name of the local host returned by gethostname(2) is used. Using this option has no effect on the /etc/crontab file and the files in
the /etc/cron.d directory, which are always run, and considered host-specific. For more information on clustering support, see cron(8).

-c  This option is only relevant if cron(8) was started with the -c option, to enable clustering support. It is used to query which host in the cluster
is currently set to run the jobs specified in the crontab files in the directory /var/spool/cron , as set using the -n option.

6. 命令示例

8,12,16 * * * /data/app/scripts/monitor/df.sh
2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh
8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh
8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh
8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh
21 * * * 每天的21:43 执行
05 * * *    每天的05:15 执行
17 * * * 每天的17:00 执行
17 * * 1 每周一的 17:00 执行 0,10 17 * * 0,2,3 每周日,周二,周三的 17:00和 17:10 执行 0-10 17 1 * * 毎月1日从 17:00到7:10 毎隔1分钟 执行
0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行
4 1 * *     毎月1日的 4:42分 执行
21 * * 1-6   周一到周六 21:00 执行 0,10,20,30,40,50 * * * * 每隔10分 执行 */10 * * * *        每隔10分 执行 * 1 * * *         从1:0到1:59 每隔1分钟 执行
1 * * *         1:00 执行
*/1 * * *        毎时0分 每隔1小时 执行
* * * *         毎时0分 每隔1小时 执行
8-20/3 * * *      8:02,11:02,14:02,17:02,20:02 执行
5 1,15 * *       1日 和 15日的 5:30 执行
 
7. 配合 notify-send 进行通知
➜  ~ crontab -l
0-5/1 9 * * * notify-send "吃早饭了,boy!"
0-5/1 11 * * * notify-send "吃水果了,boy!"
0-5/1 13 * * * notify-send "吃午饭了,boy!"
0-5/1 15 * * * notify-send "吃下午茶了,boy!"
0-5/1 18 * * * notify-send "下班了,boy!"
保持更新,转载请表明出处。
 

crontab 命令使用的更多相关文章

  1. Linux 设置定时任务crontab命令

    1.  crontab命令概念 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使 ...

  2. linux命令:crontab命令(转)

    一.crond简介 二.crond服务 三.crontab命令详解 四.使用注意事项 linux系统是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作 ...

  3. linux下crontab命令的使用

    1.crond crond是Linux下的一个守护进程,与windows下的计划任务类似,当操作系统安装完成后,默认会安装并启动进程,Linux下的任务调度分为两类,系统任务调度和用户任务调度. 系统 ...

  4. linux定时执行任务crontab命令用法

    linux系统的定时任务是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外, 由于使用者自己也可以设置计划任务,所 ...

  5. 每天一个linux命令(50):crontab命令

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  6. 【转】定时自动启动任务crontab命令用法

    每个操作系统都有它的自动定时启动程序的功能,Windows有它的任务计划,而Linux对应的功能是crontab. crontab简介 crontab命令常见于Unix和类Unix的操作系统之中,用于 ...

  7. crontab命令

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  8. [转]crontab命令指南

    原文链接:http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html 前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划 ...

  9. [linux]crontab 命令执行问题

    在服务器上设置了一个R脚本的crontab任务,死活不执行.在网上搜了很久,终于解决了. 这里主要说一下crontab异常时,该如何排查. 假设cron命令为:* * * * Rscript /you ...

  10. Linux crontab 命令详解

    在 Linux 中,任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行.红帽企业 Linux 预配置了对重要系统任务的运行,以便使系统能够时时被更新.譬如,被 locat ...

随机推荐

  1. HTTPS原理简述

    角色:   A,B,Server,Client,中间窃听者,数字证书签发机构(CA) 工具:对称加密算法,非对称加密算法,数字签名,数字证书 第一步,爱丽丝给出协议版本号.一个客户端生成的随机数(Cl ...

  2. django2.1---终端打印orm转义的sql语句

    print(connection.queries)可以打印转义后的sql语句 例子: from django.http import JsonResponse,HttpResponse from .m ...

  3. 自己实现一个一致性 Hash 算法

    前言 在前文分布式理论(八)-- Consistent Hash(一致性哈希算法)中,我们讨论了一致性 hash 算法的原理,并说了,我们会自己写一个简单的算法.今天就来写一个. 普通 hash 的结 ...

  4. 并发编程之 Java 内存模型 + volatile 关键字 + Happen-Before 规则

    前言 楼主这个标题其实有一种作死的味道,为什么呢,这三个东西其实可以分开为三篇文章来写,但是,楼主认为这三个东西又都是高度相关的,应当在一个知识点中.在一次学习中去理解这些东西.才能更好的理解 Jav ...

  5. StreamReader 的ReadLine,Read,ReadToEnd方法

    1.ReadLine() 当遇到\n \r 或者是\r\n的时候 此方法返回这前面的字符串,然后内部的指针往后移一位下次从新的地方开始读,直到遇到数据的结尾处返回null,所以经常这样使用       ...

  6. [android] 帧动画和补间动画

    逐帧显示一张图片,连起来成为动画 在res/drawable/目录下,创建一个xxx.xml的文件 添加<animation-list>节点,设置是否循环android:oneshot:” ...

  7. java 传入list集合 返回树形菜单,for循环遍历

    public List<SysPermissionVO> getTreeMenu(List<SysPermissionVO> list,SysPermissionVO sysP ...

  8. SSM整合的pom.xml依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. 【学习笔记】---老男孩学Python,day1

    老早同学就推荐自己学编程了,因为各种事耽误了几年的时间,也可以说自己没有居安思危的意识吧… 直到今年2月份决定掏钱学线上课,但是又被兼职打断了,公司忙,兼职事多,拖来拖去只能把课程延期.这一拖就到了五 ...

  10. JavaScript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prompt()

    第一种:alert()方法 alert()方法是这三种对话框中最容易使用的一种,她可以用来简单而明了地将alert()括号内的文本信息显示在对话框中,我们将它称为警示对话框,要显示的信息放置在括号内, ...