且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux系统管理(一):定时任务Crontab命令

更新时间:2022-09-16 21:17:49

前言:


我们知道,at命令是用户直接给定一个时间点去执行某特定的任务,对于一些日常都需要去执行的命令,我们不能每天都去执行一次,所以,Linux提供了一个循环运行的例行工作命令“crontab”,它是由cron(crond)这个系统服务去控制的,IT运维人员通过配置crontab配置文件去实现循环运行,目前它是互联网很常用的技术。(我们也可以把它理解为Windows下的“任务计划程序”),接下来,我们看一下怎么去使用这个命令:


Crontab的语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~]# crontab [-u username] [-l|-e|-r]
选项与参数:
-u  : 只有 root 才能进行这个任务,帮其他使用者创建/移除 crontab 工作任务
-e  : 编辑 crontab 的任务
-l  : 查看 crontab 的任务
-r  : 移除所有的 crontab 的任务,若仅要移除一项,请用 -e 去编辑。
例如:每两分钟往/tmp/output.txt文件写入”This is only for crontab test
[root@localhost ~]# crontab -e
*/2 * * * * echo "This is only for crontab test" >> /tmp/output.txt
执行结果
[root@localhost ~]# ls -lh /tmp/output.txt
-rw-r--r--. 1 root root 30 Jan 14 17:46 /tmp/output.txt
[root@localhost ~]# ls -lh /tmp/output.txt
-rw-r--r--. 1 root root 60 Jan 14 17:48 /tmp/output.txt
[root@localhost ~]# cat /tmp/output.txt
This is only for crontab test
This is only for crontab test
执行日志查询: 日志的存储位置:/var/log/cron
[root@localhost log]# cat cron | tail 3
Jan 14 17:46:01 localhost CROND[4975]: (root) CMD (echo "This is only for crontab test" >> /tmp/output.txt )
Jan 14 17:48:02 localhost CROND[5023]: (root) CMD (echo "This is only for crontab test" >> /tmp/output.txt )
Jan 14 17:50:01 localhost CROND[5070]: (root) CMD (echo "This is only for crontab test" >>

一些特殊字符的使用

1
2
3
逗号(,)
例子:每天的下午6点与8点备份/etc/目录至/backup/
      0 6,8 * * * cp /etc /backup/
1
2
3
减号(-)
例子:每天的9点至12的每小时的30分钟备份/etc/datamakr/backup/log/
      30 9-12 * * * cp /etc/datamakr /backup/log/

1
2
斜线(\n)
例子:每分钟,如上面的例子*/2每两分钟执行一次命令

Crontab命令的使用限制


/etc/cron.allow:将可以使用 crontab 的帐号写入其中,若不在这个文件内的使用者则不可使用 crontab;

/etc/cron.deny:将不可以使用 crontab 的帐号写入其中,若未记录到这个文件当中的使用者,就可以使用 crontab 。

以优先顺序来说, /etc/cron.allow 比 /etc/cron.deny 要优先, 而判断上面,这两个文件只选择一个来限制而已,因此,建议你只要保留一个即可, 免得影响自己在配置上面的判断!一般来说,系统默认是保留 /etc/cron.deny ,你可以将不想让他运行 crontab 的那个使用者写入 /etc/cron.deny 当中,一个帐号一行!该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。


Crontab配置文件的详解


首先我们来看看Crontab配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash    #使用的shell文件
PATH=/sbin:/bin:/usr/sbin:/usr/bin   #命令的搜索路径
MAILTO=root   #将结果以邮件的方式转给给谁,默认是管理员root(也可以是输入邮箱地址)
# 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

备注:为什么我们上面利用“crontab -e”执行的命令没有保存在这个文件呢?原因是“crontab -e”是针对使用者的cron来设计的,如果您想要一些日常需能够循环运行,那么直接编辑“/etc/crontab”这个文件,cron会每隔分钟的去读取一次/etc/crontab 与 /var/spool/cron 里面的数据内容,所以,只要你编辑完 /etc/crontab 这个文件,并且将他储存之后,那么 cron 的配置就自动的会来运行了。


选项 Minutes hour day-of-month month of year day of week Command
意义 分钟 小时 日期 月份  命令
取值范围 0-59 0-23 1-31 1-12 0-6,0,7代表周日


Cron服务进程的状态查看

1
2
CentOS 7: Systemctl status crond
CentOS 6: service crond status

重启

1
/etc/init.d/crond restart


参考文献:

http://vbird.dic.ksu.edu.tw/linux_basic/0430cron_3.php

http://www.php-note.com/article/detail/786











本文转自 Nico_Lv 51CTO博客,原文链接:http://blog.51cto.com/nearlv/1735243,如需转载请自行联系原作者