一天一条Linux指令-crontab

用途说明

crontab命令用于设置例行任务,类似于Windows中的任务计划。我们常用它来设置如下定时执行的任务:

  • 进行时间同步:ntpdate
  • 进行数据统计:mysql, sqlplus
  • 检查磁盘空间:df
  • 监控CPU使用:vmstat
  • 检查某些程序是否还在运行,重新启动:ps, service xxx start
  • 杀掉某些运行时间过长的进程:kill
  • 定时发送邮件:mail
  • 清理日志:find & rm
  • 清除数据库中的历史记录:delete from xxx where date <= n days before
  • 备份数据库:mysqldump
  • 自动系统更新:yum
  • ...

 

常用参数

格式:crontab -l

参数小写L,用于显示crontab中设置的任务情况。

 

格式:crontab -e

使用文本编辑器(通常是vi)编辑任务列表。

 

任务格式要点如下:

以#号开头的行是注释:但是与shell脚本不同的是,注释只能单独写在一行,不能与任务或环境变量设置写在同一行。

空行将被忽略;

环境变量设置:name = value,与shell脚本不同的是:等号前后可以加空格。

任务由六部分组成:小时 分钟 日期 月份 星期 程序及参数

前面五个部分用来设置任务执行的频次:

可以设置为*,比如分钟设置为*是表示每分钟;

也可以设置为具体的数值,比如小时设置为9表示9点的时候;

还可以设置多个数值,以逗号分隔,比如日期设置为5,15,25表示匹配这三个日期之一;

但是有一点要切记:前面四项的关系之间为and的关系,即需要同时满足;但星期那一项与前面月份日期是or的关系(被人称之为“冲突的逻辑”)。

 

任务的六个组成部分:

1)分(0-59) 
2)时(0-23) 
3)日(1-31) 
4)月(1-23,或者英文名) 
5)周(0-7,或者英文名,0和7均表示周日) 
6)要执行的内容:包括程序名称和参数,但要注意%的使用,后面有讲到。

 

关于前五段:时间和日期 
1)表示任意:* 号表示 “任意”(first-last)。
2)指定数字,表示指定的时间。
3)指定段,表示“开始-结束”内包含的数:比如3-6,表示3,4,5,6
4)指定列表:比如 “1,2,3,4″,”0-4,8-12″
5)指定“步长”:8-14/2 表示8,10,12,14

 

关于百分号%

%意味着命令行的结束。%后面的内容将被当做命令的标准输入,同时%又相当于换行。

man 5 crontab 写道
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the
line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL vari-
able of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed
into newline characters, and all data after the first % will be sent to the command as standard input.

 

“%”在crontab文件中,有“结束命令行”、“换行”、“重定向”的作用,比如:
0 22 * * 1-5 mail -s “It’s 10:00 pm” joe%Joe,%%Where are your kids?%
将会在周一至周五的22:00发送一分内容为:
Joe,<换行>

<换行>

Where are your kids?<换行>

如果在命令行中确实需要%,比如date命令中指定日期时间的输出格式,那么就需要加上\进行转义。

0 4 * * *  /opt/cron/mysql_dump.sh ? ?> /srv/logs/`date +\%y-\%m-\%d`.dump.log

 

关于crontab的帮助:

man crontab      命令行的帮助

man 5 crontab   任务定义格式

 

crontab手册页中的示例 (EXAMPLE CRON FILE)

man 5 crontab 写道
# use /bin/sh to run commands, no matter what /etc/passwd says
SHELL=/bin/sh
# mail any output to ‘paul’, no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"
 

 

符号@开头的特殊时间 ,比如 @reboot表示启动之后执行一次。

man 5 crontab 写道
These special time specification "nicknames" are supported, which replace the 5 initial time and date fields,
and are prefixed by the ’@’ character:
@reboot : Run once, at startup.
@yearly : Run once a year, ie. "0 0 1 1 *".
@annually : Run once a year, ie. "0 0 1 1 *".
@monthly : Run once a month, ie. "0 0 1 * *".
@weekly : Run once a week, ie. "0 0 * * 0".
@daily : Run once a day, ie. "0 0 * * *".
@hourly : Run once an hour, ie. "0 * * * *".

 

用于crontab的shell脚本编写注意事项:

1)执行位置:最好在前面加上 cd $(dirname $0) || exit 1   表示更改当前工作目录到脚本所在目录

2)环境变量:最好在前面加上 . /etc/profile    表示将/etc/profile设置的环境变量引入

脚本样本如下所示:

Bash代码   收藏代码
  1. #!/bin/sh  
  2.   
  3. cd $(dirname $0) || exit 1  
  4.   
  5. . /etc/profile  
  6.   
  7. # 剩下的代码  

 

要使crontab设置的任务有效的执行,必须确保crond服务在运行。

[root@smsgw mhr_server]# service crond 
Usage: /etc/init.d/crond {start|stop|status|reload|restart|condrestart}
[root@smsgw mhr_server]# service crond status 
crond (pid 28553 28543 28337 28328 27900 14497 1865) is running...
[root@smsgw mhr_server]#

如果不在运行,可以执行下面的命令启动:

service crond start

如果要确保系统重启之后该服务自动执行,可以执行如下命令:

chkconfig crond on

 

使用示例

示例一 显示crontab设置的任务

[root@web ~]# crontab -l 

# 2010.06.30 Tomcat监控
* * * * * mj.sh /opt/apache/apache-tomcat-6.0.26/bin tomcat './startup.sh'

# 2010.07.21 邮件代理服务器
* * * * * mj.sh /opt/omc08/src/ems_proxy ems_proxy 'make start'

[root@web ~]#

 

示例二 日期和星期同时设置

错误示例:每月6到12号中的星期天的4点正,执行备份脚本。做了如下crontab项。 
0 4 6-12 * 0 /root/work190/mhr_server/backup_workplace_sent.sh 
在crontab里面如果日期与星期同时设置,是“或”的关系,也就是说上面的设置的含义是每月的6到12号或者星期天,这些日子的4点执行备份脚本。

怎么样才能实现这个需求呢?没办法,只能在脚本中进行判断。

 

示例三 定时关机

为了节约用电,在每天下班之后最好关闭开发服务器,如果忘记了的话,就可以叫crontab帮忙啦。

30 18 * * * poweroff

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值