linux下查看定时指令,Linux系统管理(一):定时任务Crontab命令

前言:

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

Crontab的语法[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" >>

一些特殊字符的使用逗号(,)

例子:每天的下午6点与8点备份/etc/目录至/backup/下

0 6,8 * * * cp /etc /backup/减号(-)

例子:每天的9点至12的每小时的30分钟备份/etc/datamakr至/backup/log/

30 9-12 * * * cp /etc/datamakr /backup/log/

斜线(\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配置文件内容[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 的配置就自动的会来运行了。

选项Minuteshourday-of-monthmonth of yearday of weekCommand

意义分钟小时日期月份周命令

取值范围0-590-231-311-120-6,0,7代表周日

Cron服务进程的状态查看CentOS 7: Systemctl status crond

CentOS 6: service crond status

重启/etc/init.d/crond restart

参考文献:

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

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值