Linux crontab定时任务配置方法(详解)
- 手机版
- 投稿中心
- 关注微信
<div class="logor fr"><a href="https://dnspod.cloud.tencent.com/act/yearendsales?from=IT&fromSource=gwzcw.762106.762106.762106" target="_blank"><img src="http://files.jb51.net/image/tengxunyun370.gif" width="370" height="60"></a>
<!-- 导航开始 -->
<div class="nav_top clearfix"><ul><li><a href="http://www.jb51.net/">网站首页</a></li><li><a href="/web/">网页制作</a></li><li><a href="/list/index_1.htm">网络编程</a></li><li><a href="/list/index_96.htm">脚本专栏</a></li><li><a href="/jiaoben/">脚本下载</a></li><li><a href="/list/index_104.htm">数据库</a></li><li><a href="/cms/">CMS教程</a></li><li><a href="/books/">电子书籍</a></li><li><a href="/pingmian/">平面设计</a></li><li><a href="/media/">媒体动画</a></li><li><p><i>其它</i><span><a href="/os/">操作系统</a><a href="/yunying/">网站运营</a><a href="/hack/">网络安全</a><a href="/shouce/">在线手册</a></span></p></li></ul>
</div>
<!--导航结束-->
- ◆◆◆◆◆高防免备案CDN◆◆◆◆◆
- 30余款阿里云产品免费6个月>>
- 1G香港云49元/美国云49元/韩国云89元
- ★美国/香港/国内/高防服务器VPS★好优云
- 【3000个备案老域名】100元起 每天更新
- 【亿恩】DELL品牌服务器,月付799元起
- 服务器租用/托管-域名空间/认准腾佑科技
- 九九数据 — 工信部认可正规资质IDC接入商
- 阿里云/腾讯云/百度云等/量大从优
- ★☆云服务器5折,天天抽红包抵扣☆★
- **香港高防10m大带宽独服,低至999元**
- 知了云,OpenStack云服务器◆5折优惠◆
- 4核独服199/16核独服360|创梦网络
- 怎样购买服务器更划算?
- 中原地区核心数据中心,首月托管免费
- ██云彩网络██100G防服务器450元
- 产品发布、创业开店、需求任务找大师兄
- 港湾网络-徐州百独16核32G 999/月~
- 免备vps20/百独799/双线350/45互联
- 韩国香港美国站群服务器 巨牛网络
- 鼎点网络百兆独享服务器仅需999元
- [香港双高防]无视CC★DDOS/堪比广东!
- 畅游网络 百独服务器 包跑满 998元
- 服务器租用 199元起
- █▇▆5M独享云主机599/年▆▇█
- 群英云服务器送10M带宽30G防御,49元起
- 好系统,装机首选好系统
- ★★★香港服务器租用100M促销★★★
- 95IDC█香港沙田CN2服务器 599/月
- 枫信科技-江苏双线10M保证-399/元
- ◆◆◆海西数据全球服务器租用◆◆◆
- 浦东数据中心上海电信4星云主机30元/月起
Linux crontab定时任务配置方法(详解)
转载 2016-11-30 投稿:jingxian 我要评论
<div id="content"><p><span style="color: #ff0000"><strong>CRONTAB概念/介绍</strong></span></p>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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: 尽量所有的文件都采用完全路径方式,避免使用相对路径。
Linux crontab定时任务配置方法(详解)相关推荐
- python定时任务crontab_【Python】Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. cron 系统调度 ...
- linux下crontab allow,【Python】Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍html crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供以后读取和执行.linux ...
- linux etc cron.deny,Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. cron 系统调度 ...
- linux中apache配置文件在哪,linux网站服务Apache的安装与配置方法详解
这篇文章介绍下linux网站服务apache的安装与配置方法,包括挂载光盘,安装http服务,管理httpd服务,httpd的配置文件几大部分.具体详情可以参考下文. 1.挂载光盘 自己习惯将光盘挂载 ...
- navicat mysql 计划任务_navicat创建MySql定时任务的方法详解
navicat创建MySql 定时任务详解 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的" ...
- ecmall php传变量,PHP_ECMall支持SSL连接邮件服务器的配置方法详解,首先,主要是ecmall使用的phpmail - phpStudy...
ECMall支持SSL连接邮件服务器的配置方法详解 首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进 ...
- hibenate5.1配置mysql_hibernate5.2的基本配置方法(详解)
目标:将Student实体对象加入数据库 1.首先需要下载三个东西:hibernate,slf4j,mysql. 2.分别取他们的包导入新建的项目中,我这里的版本是:hibernate-release ...
- oracle tns 代理配置_oracle数据库tns配置方法详解
TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器. TNS ...
- server2003-多域间林之间信任配置方法详解(附图)
多域间林之间信任配置方法详解 -----冯刚 DATA:20091018 实验拓朴: 实验 环境描述: MICHAEL公司日常办使用的域是"michael.com.cn"(林1), ...
最新文章
- 如何将Vision Transformer应用在移动端?
- ubuntu-基本命令篇-12-磁盘管理
- 赛灵思PLL重配置一PLL配置介绍___S6器件族
- 从“抢跑”到领跑,滴滴旗下橙心成社区团购最“优选”
- Unity3D 游戏引擎之IOS高级界面发送消息与Unity3D消息的接收(九)
- perl大骆驼和小骆驼_快速的骆驼和云消息传递
- java商品信息管理系统代码_[源码分享]学生信息管理系统(管理员)
- 全球首发!计算机视觉Polygon Mesh Processing总结8——Remeshing Global Structure和Correspondences
- 职称英语职称计算机如何折算为学时,职称英语成绩可折算学时 或成考试新方向...
- 深度学习导论 - 读李宏毅《1天搞懂深度学习》
- 如何在快手批量下载高清不含水印视频
- html 文件怎么改类型,怎么更改文件类型
- linux打开pkg文件怎么打开,linux中怎么打开tar.rar.gz文件
- java 小游戏Flappy bird的源码以及图片素材
- 兔子繁殖问题Java实现
- 电脑升级建议(加固态硬盘还是内存、CPU、显卡)
- W-GAN系 (Wasserstein GAN、 Improved WGAN)
- marvelous designer 10安装使用教程(附快捷键)
- Dell服务器出错代码和含义
- win7无法通过网页登录ftp服务器,win7不能登录ftp服务器配置