Linux 下 Crontab 命令使用详解 定时任务
一. Crontab 介绍
crontab命令的功能是在一定的时间间隔调度一些命令的运行。
1.1 /etc/crontab 文件
在/etc文件夹下有一个crontab文件,这里存放有系统运行的一些调度程序。每一个用户能够建立自己的调度crontab。
如:
[root@dave ~]# cat /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
1.2 /etc/cron.deny 和 /etc/cron.allow 文件
/etc/cron.deny 表示不能使用crontab 命令的用户
/etc/cron.allow 表示能使用crontab的用户。
假设两个文件同一时候存在,那么/etc/cron.allow 优先。
假设两个文件都不存在,那么唯独超级用户能够安排作业。
每一个用户都会生成一个自己的crontab 文件。这些文件在/var/spool/cron文件夹下:
如:
[root@dave ~]# cd /var/spool/cron
[root@dave cron]# ls
oracle root
我们直接查看这个文件,里面的内容和相应用户显示的crontab -l 一致。
[root@dave cron]# cat oracle
00 6 * * * /u02/scripts/del_st_archive.sh >/u02/scripts/del_st_arch.log 2>&1
[root@dave cron]# cat root
0 12 * * * /root/bin/sync-clock.sh
[root@dave cron]#
二. Crontab 使用说明
2.1 Crontab语法
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
-s (selinux context)
当中,file是命令文件的名字。假设在命令行中指定了这个文件,那么运行crontab命令,则将这个文件复制到crontabs文件夹下;假设在命令行中沒有制定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将他们也存放在crontab文件夹下。
帮助:
[root@dave ~]# man crontab
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 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 edi-tor, 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 / replacement 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)
2.2 Crontab 格式说明
我们能够用crontab -e 增加要运行的命令。 命令运行的结果,不管是标准输出还是错误输出,都将以邮件形式发给用户。
增加的命令必须以例如以下格式:
* * * * * /command path
前五个字段能够取整数值,指定何时開始工作,第六个域是字符串,即命令字段,当中包含了crontab调度运行的命令。 各个字段之间用spaces和tabs切割。
前5个字段分别表示:
分钟:0-59
小时:1-23
日期:1-31
月份:1-12
星期:0-6(0表示周日)
还能够用一些特殊符号:
*: 表示不论什么时刻
,: 表示切割
-:表示一个段,如第二端里: 1-5,就表示1到5点
/n : 表示每一个n的单位运行一次,如第二段里,*/1, 就表示每隔1个小时运行一次命令。也能够写成1-23/1.
一些演示例子:
00 8,12,16 * * * /data/app/scripts/monitor/df.sh
30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh
10 8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh
10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh
10 8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh
43 21 * * * 21:43 运行
15 05 * * * 05:15 运行
0 17 * * * 17:00 运行
0 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 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 运行
42 4 1 * * 毎月1日的 4:42分 运行
0 21 * * 1-6 周一到周六 21:00 运行
0,10,20,30,40,50 * * * * 每隔10分 运行
*/10 * * * * 每隔10分 运行
* 1 * * * 从1:0到1:59 每隔1分钟 运行
0 1 * * * 1:00 运行
0 */1 * * * 毎时0分 每隔1小时 运行
0 * * * * 毎时0分 每隔1小时 运行
2 8-20/3 * * * 8:02,11:02,14:02,17:02,20:02 运行
30 5 1,15 * * 1日 和 15日的 5:30 运行
2.3 & 后台运行命令
当在前台运行某个作业时,终端被该作业占领;而在后台运行作业时,它不会占领终端。能够使用&命令把作业放到后台运行。
如:
30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh &
在后台运行作业时要当心:须要用户交互的命令不要放在后台运行,由于这样你的机器就会在那里傻等。
只是,作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。假设放在后台运行的作业会产生大量的输出,最好使用以下的方法把它的输出重定向到某个文件里:
如:
command >out.file 2>&1 &
在这个样例中,2>&1表示全部的标准输出和错误输出都将被重定向到一个叫做out.file 的文件里。
2.4 2>&1 含义
先看一个样例:
0 2 * * * /u01/test.sh >/dev/null 2>&1 &
这句话的意思就是在后台运行这条命令,并将错误输出2重定向到标准输出1,然后将标准输出1全部放到/dev/null 文件,也就是清空。
在这里有有几个数字的意思:
0表示键盘输入
1表示标准输出
2表示错误输出.
我们也能够这样写:
0 2 * * * /u01/test.sh >/u01/out.file & --这里没写,默认是1
0 2 * * * /u01/test.sh 1>/u01/out.file &
0 2 * * * /u01/test.sh 2>/u01/out.file &
0 2 * * * /u01/test.sh 2>/u01/out.file 2>&1 &
将tesh.sh 命令输出重定向到out.file, 即输出内容不打印到屏幕上,而是输出到out.file文件里。
2>&1 是将错误输出重定向到标准输出。 然后将标准输入重定向到文件out.file。
&1 表示的是文件描写叙述1,表示标准输出,假设这里少了&就成了数字1,就表示重定向到文件1。
& :后台运行
測试:
ls 2>1 : 不会报沒有2文件的错误,但会输出一个空的文件1;
ls xxx 2>1: 沒有xxx这个文件的错误输出到了1中;
ls xxx 2>&1: 不会生成1这个文件了,只是错误跑到标准输出了;
ls xxx >out.txt 2>&1 == ls xxx 1>out.txt 2>&1; 由于重定向符号>默认是1,这句就把错误输出和标准输出都传到out.txt 文件里。
2.5 2>&1写在后面的原因
格式:command > file 2>&1 == command 1> file 2>&1
首先是command > file将标准输出重定向到file中, 2>&1 是标准错误拷贝了标准输出,也就是相同被重定向到file中,终于结果就是标准输出和错误都被重定向到file中。
假设改成: command 2>&1 >file
2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file 后输出才被重定向到file,但标准错误仍然保持在终端。
------------------------------------------------------------------------------
Blog: http://blog.csdn.net/tianlesoftware
网上资源: http://tianlesoftware.download.csdn.net
相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx
DBA1 群:62697716(满); DBA2 群:62697977(满)
DBA3 群:62697850 DBA 超级群:63306533;
聊天 群:40132017
--加群须要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请
Linux 下 Crontab 命令使用详解 定时任务的更多相关文章
- Linux下find命令用法详解
Linux下find命令用法详解 学神VIP烟火 学神IT教育:XueGod-IT 最负责任的线上直播教育平台 本文作者为VIP学员 烟火 第一部分:根据文件名查找 1.在当前目录 ...
- linux下sort命令使用详解---linux将文本文件内容加以排序命令
转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...
- Linux下crontab命令添加Kettle作业定时任务
1. 确保作业 $KETTLE_HOME/kitchen.sh -file=/data1/testdata/testkjb.kjb 或转换 $KETTLE_HOME/pan.sh -file=/dat ...
- LINUX下分区命令Parted详解
通常划分分区工具我们用的比较多是fdisk命令,但是现在由于磁盘越来越廉价,而且磁盘空间越来越大.而fdisk工具他对分区是有大小限制的,它只能划分小于2T的磁盘.现在的磁盘空间已经远远大于2T,有两 ...
- linux下top命令参数详解
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. 内存信息.内容如下: Mem: 191272k to ...
- Linux下ls命令使用详解(转)
说明:我们在linux下使用ll时,其实就是ls -l.ls才是最终的命令程序. ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,List即列表的意 ...
- Linux下diff命令用法详解
大家好,我是良许. 我们在平时工作的时候,经常要知道两个文件之间,以及同个文件不同版本之间有何异同点.在 Windows 下,有 beyond compare 这个好用的工具,而在 Linux 下,也 ...
- Ubuntu(Linux) 下 unzip 命令使用详解
1.功能作用:解压缩zip文件 2.位置:/usr/bin/unzip 3.格式用法:unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist ...
- linux下xargs命令用法详解 【转】
转自:http://blog.chinaunix.net/uid-128922-id-289992.html xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xa ...
随机推荐
- C2:抽象工厂 Abstract Factory
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 应用场景: 一系列相互依赖的对象有不同的具体实现.提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合 UM ...
- 关于Laravel5.2在php5.3.6X和在php7.1.10下的内存溢出
php5.3.6X是编译安装,在debug模式下,频繁报出内存泄露警告 php7.1.10下则不会有此错误. 顺便提下:测试发现ThinkPHP也不会有该内存泄露警告! 希望知道如何解决该问题的童鞋能 ...
- js 判断是否包含
1.判断一个数组中是否包含某元素 arr.indexOf(val) > -1 // true 包含 false 不包含 arr.includes(val) // ES7 true 包含 fals ...
- DNS主从服务器
一.目的: 我们知道,DNS服务器在网络服务中可能出现故障当机等状况,会导致DNS服务瘫痪,显然在实际的网络应用中我们不希望出现这种状况,所有我们就要配置从 服务器来在主DNS服务器出现故障时代替他来 ...
- group by having和connect by
--使用group by 子句对数据进行分组:对group by 子句形成的组运行聚集函数计算每一组的值:最后用having 子句去掉不符合条件的组.--having 子句中的每一个元素也必须出现在s ...
- Android_Fragment_Fragment详解
Android_Fragment_Fragment详解 分类: Android基础2013-10-03 08:23 92人阅读 评论(0) 收藏 举报 AndroidFragmentFragmen ...
- mfs客户端挂载
1.安装fuse yum install fuse fuse-devel 2.加载fuse模块 modprobe fuse 3.创建mfs用户 useradd mfs -s /sbin/nologin ...
- vbox克隆虚拟机,网卡启动报错“Device eth0 does not seem to be present”
vbox克隆虚拟机,网卡启动报错"Device eth0 does not seem to be present". 须要看以下三个地方:确保文件名称,设备名.mac地址都一致. ...
- Android 动画分析学习笔记
一:分类: Android动画分三种:view动画(对场景中的对象不断做图像变换<平移,缩放,旋转,透明度>).帧动画(顺序播放一系列图像产生动画效果).属性动画(动态改变对象属性). 二 ...
- js 温故而知新 webkitTransitionEnd 监听Transition动画结束事件
css3的过渡属性transition,在动画结束时,也存在结束的事件:webkitTransitionEnd; 注意:transition,也仅仅有这一个事件. http://www.runoob. ...