Crontab表达式

来自tomtalk
跳转至: 导航搜索

CentOS安装crontab

[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs
[root@CentOS ~]# service crond start

禁止Crontab产生邮件

在crontab末尾加上

>/dev/null 2>&1.

或者

&> /dev/null

例如

0 1 5 10 * /path/to/script.sh >/dev/null 2>&1

0 1 5 10 * /path/to/script.sh &> /dev/null

另外一种方法是编辑crontab

crontab -e

在第一行加入

MAILTO=""

保存退出

这样做的好处是,可以避免 /var/mail/root 体积快速增长

crontab表达式

位置一般在/var/spool/cron/下,如果你是root用户,那下面有个root文件。

crontab文件位置: /etc/crontab

前面的五个星号分别表示 分 时 日 月 周,commond表示你要操作的命令

分(1-59)(*或*/1表示每分钟)

时(1-23)(0表示0点)

日(1-31)

月(1-12)

周(1-6)(0表示周日)

使用方式 :

crontab file [-u user]-用指定的文件替代目前的crontab。

crontab-[-u user]-用标准输入替代目前的crontab.

crontab-1[user]-列出用户目前的crontab.

crontab-e[user]-编辑用户目前的crontab.

crontab-d[user]-删除用户目前的crontab.

crontab-c dir- 指定crontab的目录。

crontab的使用例子

*/20 6-12 * 12 * /usr/bin/backup                      #在 12 月内, 每天的早上 6 点到 12 点中,每隔 20 分钟执行一次 /usr/bin/backup :
30 21 * * * /usr/local/etc/rc.d/lighttpd restart      #每晚的21:30重启apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart #每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart     #每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart #每天18 : 00至23 : 00之间每隔30分钟重启apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart       #每星期六的11 : 00 pm重启apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart      #每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart   #晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart #每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart      #一月一号的4点重启apache
 
*/20 6-12   *       12  *       /usr/bin/backup                      #在 12 月内, 每天的早上 6 点到 12 点中,每隔 20 分钟执行一次 /usr/bin/backup :
30   21     *       *   *       /usr/local/etc/rc.d/lighttpd restart #每晚的21:30重启apache。
45   4      1,10,22 *   *       /usr/local/etc/rc.d/lighttpd restart #每月1、10、22日的4 : 45重启apache。
10   1      *       *   6,0     /usr/local/etc/rc.d/lighttpd restart #每周六、周日的1 : 10重启apache。
0,30 18-23  *       *   *       /usr/local/etc/rc.d/lighttpd restart #每天18 : 00至23 : 00之间每隔30分钟重启apache。
0    23     *       *   6       /usr/local/etc/rc.d/lighttpd restart #每星期六的11 : 00 pm重启apache。
*    */1    *       *   *       /usr/local/etc/rc.d/lighttpd restart #每一小时重启apache
*    23-7/1 *       *   *       /usr/local/etc/rc.d/lighttpd restart #晚上11点到早上7点之间,每隔一小时重启apache
0    11     4       *   mon-wed /usr/local/etc/rc.d/lighttpd restart #每月的4号与每周一到周三的11点重启apache
0    4      1       jan *       /usr/local/etc/rc.d/lighttpd restart #一月一号的4点重启apache

Linux下实现秒级定时任务的两种方案(crontab 每秒运行)

第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间。

while true ;do


command


sleep XX //间隔秒数


done


第二种方案,使用crontab。

我们都知道crontab的粒度最小是到分钟,但是我们还是可以通过变通的方法做到隔多少秒运行一次。

以下方法将每20秒执行一次

crontab -e

  • * * * * /bin/date
  • * * * * sleep 20; /bin/date
  • * * * * sleep 40; /bin/date

说明:需要将/bin/date更换成你的命令即可


这种做法去处理隔几十秒的定时任务还好,要是每1秒运行一次就得添加60条记录。。。如果每秒运行还是用方案一吧。