Linux crontab定时任务配置方法(详解)
作者:jingxian 发布时间:2021-01-10 06:25:17
CRONTAB概念/介绍
crontab命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。
cron 系统调度进程。 可以使用它在每天的非高峰负荷时间段运行作业,或在一周或一月中的不同时段运行。cron是系统主要的调度进程,可以在无需人工干预的情况下运行作业。crontab命令允许用户提交、编辑或删除相应的作业。每一个用户都可以有一个crontab文件来保存调度信息。系统管理员可以通过cron.deny 和 cron.allow 这两个文件来禁止或允许用户拥有自己的crontab文件。
1: crontab文件一般位于/etc/下,这里面存放系统运行的的调度程序。
[root@localhost cron]# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
2:每个用户都会生成一个自动生成一个自己的crontab文件,一般位于/var/spool/cron目录下
[root@localhost cron]# cd /var/spool/cron
[root@localhost cron]# ls
oracle root
如果你用命令crontab -r 就会删除当前用户的crontab文件,例如你切换到oracle账号下,执行了该命令,那么/var/spool/cron/oracle文件就会删除,如果要创建该文件只需要用crontab -e命令即可。注意,普通用户一般没有权限访问/var/spool/cron
3:cron.deny 与 cron.allow文件
/etc/cron.deny 表示不能使用crontab 命令的用户
/etc/cron.allow 表示能使用crontab的用户。
默认情况下,cron.allow文件不存在。如果两个文件同时存在,那么/etc/cron.allow 优先。如果两个文件都不存在,那么只有超级用户可以安排作业。
但是让人纳闷的是cron.deny文件是空的,如下所示
[oracle@localhost etc]$ more cron.deny
4:cron.hourly,cron.daily,cron.monthly,cron.weekly
[oracle@localhost etc]$ ls -lrt cron*
-rw-r--r-- 1 root root 255 Sep 21 2004 crontab
-rw-r--r-- 1 root root 0 May 16 2008 cron.deny
cron.hourly:
total 0
cron.monthly:
total 8
-rwxr-xr-x 1 root root 278 Sep 29 2004 0anacron
cron.weekly:
total 16
-rwxr-xr-x 1 root root 277 Sep 29 2004 0anacron
-rwxr-xr-x 1 root root 414 Jun 6 2007 00-makewhatis.cron
cron.daily:
total 60
-rwxr-xr-x 1 root root 286 Aug 14 2004 tmpwatch
-rwxr-xr-x 1 root root 276 Sep 29 2004 0anacron
-rwxr-xr-x 1 root root 2133 Dec 1 2004 prelink
-rwxr-xr-x 1 root root 180 May 2 2006 logrotate
-rwxr-xr-x 1 root root 418 Jun 6 2007 00-makewhatis.cron
-rwxr-xr-x 1 root root 104 Aug 2 2007 rpm
-rwxr-xr-x 1 root root 121 Aug 22 2007 slocate.cron
lrwxrwxrwx 1 root root 28 May 16 2008 00-logwatch -> ../log.d/scripts/logwatch.pl
cron.d:
total 12
-rw------- 1 root root 366 Jun 12 2007 sa-update
-rw-r--r-- 1 root root 188 Jul 18 2007 sysstat
CRONTAB在线手册
注意:不同版本的Linux系统,可能crontab手册内容有所出入,请以实际版本为准。
[oracle@localhost ~]$ man crontab | more
CRONTAB(1) CRONTAB(1)
NAME
crontab - maintain crontab files for individual users (ISC Cron V4.1)
SYNOPSIS
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
DESCRIPTION
Crontab is the program used to install, deinstall or list the tables
used to drive the cron(8) daemon in ISC Cron. Each user can have their
own crontab, and though these are files in /var/spool/ , they are not
intended to be edited directly. For SELinux in mls mode can be even
more crontabs - for each range. For more see selinux(8).
If the cron.allow file exists, then you must be listed therein in order
to be allowed to use this command. If the cron.allow file does not
exist but the cron.deny file does exist, then you must not be listed in
the cron.deny file in order to use this command. If neither of these
files exists, only the super user will be allowed to use this command.
OPTIONS
-u It specifies the name of the user whose crontab is to be
tweaked. If this option is not given, crontab examines "your"
crontab, i.e., the crontab of the person executing the command.
Note that su(8) can confuse crontab and that if you are running
inside of su(8) you should always use the -u option for safety's
sake. The first form of this command is used to install a new
crontab from some named file or standard input if the pseudo-
filename "-" is given.
-l The current crontab will be displayed on standard output.
-r The current crontab will be removed.
-e This option is used to edit 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 It will append the current SELinux security context string as an
MLS_LEVEL setting to the crontab file before editing / replace-
ment occurs - see the documentation of MLS_LEVEL in crontab(5).
SEE ALSO
crontab(5), cron(8)
FILES
/etc/cron.allow
/etc/cron.deny
STANDARDS
The crontab command conforms to IEEE Std1003.2-1992 (‘‘POSIX''). This
new command syntax differs from previous versions of Vixie Cron, as
well as from the classic SVR3 syntax.
DIAGNOSTICS
A fairly informative usage message appears if you run it with a bad
command line.
AUTHOR
Paul Vixie <vixie@isc.org>
4th Berkeley Distribution 16 Januar 2007 CRONTAB(1)
CRONTAB语法及应用
1:查看当前用户的定时任务
[oracle@localhost ~]$ crontab -l
* * * * * /home/oracle/test.sh >/dev/null 2>&1
2:编辑当前用户的定时任务
可以在编辑状态修改、删除、新增一些定时任务。注释一般用#
[oracle@localhost ~]$ crontab -e
3:删除当前用户的定时任务
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
4:
如下所示,一般crontab文件里面的定时任务格式如下所示:
59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1
crontab 文件中每个条目中各个域的意义和格式:
第一列 分钟: 1——59
第二列 小时: 1——23(0表示子夜)
第三列 日 : 1——31
第四列 月 : 1——12
第五列 星期: 星期0——6(0表示星期天,1表示星期一、以此类推)
第六列 要运行的命令
我们暂且用C1、C2、C3、C4、C5、C6代表这六列,前面五列通过组合方式来决定执行脚本的频率,最小频率为每分钟执行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四种形式来表示:
当 C1 为 * 时表示每分钟都要执行脚本,C2 为 * 时表示每小时都要执行程式,依次类推.....
当 C1 为 T1-T2 时表示从第 T1 分钟到第 T2 分钟这段时间内要执行,C2 为 T1-T2 时表示从第 T1 到第 T2 小时都要执行,依次类推....
当 C1 为 */n 时表示每 n 分钟的时间间隔执行一次,C2 为 */n 表示每隔n小时的间隔执行一次,依次类推.....
当 C1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,C2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,依次类推....
下面列举几个例子供大家参考
1: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1
表示每天23点59分执行脚本/home/oracle/scripts/alert_log_archive.sh
2: */5 * * * * /home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1
表示每5分钟执行一次脚本/home/oracle/scripts/monitoring_alert_log.sh
3: 0 20 * * 1-5 mail -s "**********" kerry@domain.name < /tmp/maildata
周一到周五每天下午 20:00 寄一封信给 kerry@domain.name
..............................................
关于 >/dev/null 2>&1 的解释:
0表示键盘输入
1表示标准输出
2表示错误输出.
我们首先创建test.sh脚本如下:
#! /bin/sh
echo "hello, everybody, now is " `date`
date >> test.txt
然后添加作业
* * * * * /home/oracle/test.sh >/home/oracle/log.txt & 默认值为1,即和下面命令一致
* * * * * /home/oracle/test.sh 1>/home/oracle/log.txt &
* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt &
* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt 2>&1 &
1,2将tesh.sh 命令输出重定向到log.txt, 即输出内容不打印到屏幕上,而是输出到log.txt文件中。如果你需要追加而不是覆盖,可以用 >>代替>
2>&1 是将错误输出重定向到标准输出。 然后将标准输入重定向到文件log.txt。
&1 表示的是文件描述1,表示标准输出,如果这里少了&就成了数字1,就表示重定向到文件1。
注意事项:
配置定时任务时,需要注意两个问题:
1: 在SHELL中设置了必要的环境变量;例如一个shell脚本手工执行OK,但是配置成后台作业执行时,获取不到ORACLE的环境变量,这是因为crontab环境变量问题,Crontab的环境默认情况下并不包含系统中当前用户的环境。所以,你需要在shell脚本中添加必要的环境变量的设置
2: 尽量所有的文件都采用完全路径方式,避免使用相对路径。
猜你喜欢
- 背景:在用jmeter压测接口的时候发现其原生的监控起来不是很友好,在网上查阅的时候发现结合influxDB和grafana,出来的报告很炫
- 北京9月29日电欧洲联盟28日发出警告,如果官方检测发现iPhone屏幕离奇 * 的原因是产品制造存在缺陷,那么iPhone将被召回。&
- 裸设备,也叫裸分区(原始分区),是一种没有经过格式化,不被Unix通过文件系统来读取的特殊字符设备。本文收集裸设备和Oracle问答20例。
- 去朋友单位时发现,该单位NETWARE服务器内存由16M升为32M,但性能没有多大改善,服务器硬盘仍然转个不停,究其原因发现其硬件内存方面虽
- 1)不要下载www.php.net上面那个Windows Installer装完了之后不能用MYSQL都不知道问题出在哪。永远修不好,没错的
- vmware虚拟机运行ubuntu等卡慢虚拟机的运行速度有一部分原因决定于你的物理机的配置,如果你的物理机配置不高,建议不要运行虚拟机,那样
- 1、查看并分配虚拟网络我们首先要知道 VMware 三种网络模式的区别。①、Bridged(桥接模式):就是将主机网卡与虚拟机虚拟的网卡利用
- 百度产品线很多,能利用起来的也有很多。如果能完美的把百度产品线都利用起来,为自己的网站带来的效果是可想而知的。今天就和大家谈谈究竟如何完美的
- 近两年来,网页游戏发展迅速,从原来单一的文字格斗形式发展到今天,出现了各种类型的网页游戏,战争策略类,休闲竞技类,模拟经营类等。从网页游戏产
- asp之家注:百度做为最具影响力的中文搜索引擎,做为个人站长都想通过百度来给自己的网站带来流量,特别是新站,因为没人知道你的网站,你也不可能
- Google广告到底是什么东东?Google Adsense是一个让各种类型的网站(包括博客)快速获得收入的方法。您只需要申请一个Googl
- 10月28日下午消息,电子商务巨头阿里巴巴今日在杭州宣布,阿里巴巴(中国)教育科技有限公司(以下简称“阿里学院”)正式揭牌成立。新成立的阿里
- 说实话,今天讲这个主题纯属纸上谈兵,请各位高人莫怪,至今惭愧没有一个像样的站,不过因为经常为别人做技术以及做空间销售过程中接触过不少站长,了
- 聚友已经总结了五种Service Unavailable的原因,第一篇是常见的4个方面的原因(原文见http://www.juyo.org/
- 微软日前承认其最新有关Windows 7的安全特性的电视广告有可能对消费者进行误导,微软表示,“用户能够在零售商店或者网络上得到更好的指导。
- 真正有用的.htaccess写法为RewriteEngine onRewriteCond %{HTTP_REFERER} !^$ [NC]R
- 成功是一种习惯,失败也是一种习惯。你的习惯无法改变,但可以用好的习惯来替代。成功很简单,只要简单的事情重复做,养成习惯,如此而已。 以下是成
- 在过去的一年里,受流氓/恶意软件感染的网站数目从每周几个增长到每周数千。在我们以前的帖子“关于恶意软件警告”以及“停止恶意软件讨论组”里,我
- 禁用不明服务一些基本的命令往往可以在保护网路安全上起到很大的作用,下面几条命令的作用就非常突出。一、检测网络连接如果你怀疑自己的计算机上被别
- 有用户问Godaddy怎么把iTunes RSS重新定位到另一位置呢?现在我们就来解决这一问题。如果你想把某个RSS feed重新定位到另外