原文出处:http://www.cnblogs.com/tracy/archive/2011/12/27/2303788.html。感谢作者的无私分享


一. 
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,但标准错误仍然保持在终端。


Linux实践篇--crontab定时任务的更多相关文章

  1. linux的crontab定时任务命令

    linux的crontab定时任务命令 cron是一个linux下的定时执行工具. 启动.停止.重启.重新载入配置/sbin/service crond start|stop|restart|relo ...

  2. linux ( crontab 定时任务命令)

    linux ( crontab 定时任务命令)    crontab 定时任务命令 linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工 ...

  3. Linux下Crontab定时任务的使用教程 以及 无法执行定时任务的解决方案

     前言 本文学习思路:Linux的corntab定时任务的使用教程  --> 定时任务无效的解决方案  Linux的corntab定时任务的使用教程 1. 首先,输入命令 打开crontab定时 ...

  4. linux,crontab定时任务中为脚本指定使用参数,crontab的脚本中是否可以带参数

    需求描述: 今天在写脚本的时候,脚本的运行需要给出几个参数,那么就考虑 在crontab写定时任务的时候,是否也是能够在脚本中,增加参数呢, 因为以前没有这么用过,所以呢,就进行一次测试. 测试过程: ...

  5. Linux之crontab定时任务

    ****crontab简介**** 简而言之呢,crontab就是一个自定义定时器. ****crontab配置文件**** 其一:/var/spool/cron/ 该目录下存放的是每个用户(包括ro ...

  6. 【Linux】crontab 定时任务

    7月份,公司海外运营国发生数据库联接未释放,造成连接池连接不足的情况, 当时查询并没有及时解决问题, 为了避免现场同事多次人工重启系统的,因此写了个shell脚本,通过crontab 实现系统重启,但 ...

  7. linux添加crontab定时任务

    1.crontab -e命令进入linux定时任务编辑界面,举个简单的例子,比如我要定时往txt文件写入 */ * * * * .txt */1就是每隔一分钟像文件写入,其他一些详细的操作大家可以去网 ...

  8. 【转】Linux之crontab定时任务命令

    1.  crontab命令概念 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使 ...

  9. 【linux】Crontab 定时任务 使用实例

    1 使用putty 登录linux 服务器 2 输入以下命令.查看已有的定时任务 crontab -l 3 输入  以下命令,进入定时任务文件 crontab -e 4  键盘 选择 i  键 进行输 ...

随机推荐

  1. 设计模式成长记(一) 抽象工厂模式(Abstract Factory)

    目录 定义 UML类图 参与者 编写代码 特点 定义 提供一个创建一系列相关或相互依赖的对象的接口,而无需指定具体的类. 使用频率: UML类图 参与者 AbstractFactory:声明一个创建抽 ...

  2. asp.net mvc 记录Action耗时

    可能有些时候需要记录Action的执行时间来优化系统功能,这时可以用过滤器来实现. 新建项目 项目名称随便取 身份验证:不进行身份验证 安装Nlog 这里使用NLog来输出日志,具体使用说明请看:ht ...

  3. for循环相关的一个问题

    首先,我在for循环外定义了两个变量. //在4条直线方向,获取预置点两端的8个点的棋盘信息 ,r=,c=;//step代表直线,r,c分别代表加上偏移量之后的坐标 ,myCount=,hisCoun ...

  4. 有关python下二维码识别用法及识别率对比分析

    最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...

  5. COGS 1299. bplusa【听说比a+b还要水的大水题???】

    1299. bplusa ☆   输入文件:bplusa.in   输出文件:bplusa.out   评测插件 时间限制:1 s   内存限制:128 MB [题目描述] 输入一个整数n,将其拆为两 ...

  6. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  7. Codeforces Round #345(Div. 2)-651A.水题 651B.。。。 651C.去重操作 真是让人头大

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. java web开发中遇到的问题及解决方案(个人学习日志,持续更新)

    转:http://blog.csdn.net/ducexu/article/details/7529613 2012.05.02   星期三 1.问题:导入的新工程,名字上出现感叹号. 原因:工程的j ...

  9. mitm6:通过IPv6攻破IPv4网络

    一.前言 虽然IPv6正在互联网上逐步推广,但在内部网络环境中使用IPv6的公司依然非常稀少.然而,大多数公司并不知道,即使他们没有主动去使用IPv6,但从Windows Vista以来,所有的Win ...

  10. Android 社区App 《窝吧》开源分享

    一整个社区模式App的整套代码,包括发布动态,添加.删除好友,添加黑名单,聊天,用户反馈等功能. 相关技术 1 底层使用Bmob后端云,官网: https://www.bmob.cn/ 2 友盟插件, ...