cron是一个[守护程序]用于在指定的时间内执行行程类的任务,每个用户都有一个 crontab 文件,来容许他们指定须要执行的内容和时间,此外,系统也有一个 crontab ,用来容许像交替日志和更新本地数据这样有规则的任务。shell
环境:Ubuntn 14.04apache
一、设置默认编辑器,不然默认编辑器是nanodom
select-editor
选择的是3 回车编辑器
二、而后输入post
crontab -e
以后就能够输入你想执行的内容了.spa
好比:*/2 * * * * date >> /home/postgres/time.log 它的意思就是每过两分钟就向time.log文件中写入固然的系统时间.rest
参数说明日志
# m h dom mon dow commandcode
m 分钟 0-59orm
h 小时 0-23
dom 天1-31
mon 月 1-12
dow 星期 1-6 0表示星期天
command 就是要执行的命令
除了这些固定值外,还能够配合星号(*),逗号(,),和斜线(/)来表示一些其余的含义:
星号 表示任意值,好比在小时部分填写 * 表明任意小时(每小时)
逗号 能够容许在一个部分中填写多个值,好比在分钟部分填写 1,3 表示一分钟或三分钟
斜线 通常配合 * 使用,表明每隔多长时间,好比在小时部分填写 */2 表明每隔两分钟。因此 */1 和 * 没有区别
1. * * * * * date >> /home/postgres/time.log # 每隔一分钟执行一次任务
2. 0 * * * * date >> /home/postgres/time.log # 每小时的0点执行一次任务,好比6:00,10:00
3. 6,10 * 2 * * date >> /home/postgres/time.log # 每月2号,每小时的6分和10分执行一次任务
4. */3,*/5 * * * * date >> /home/postgres/time.log # 每隔3分钟或5分钟执行一次任务,好比10:03,10:05,10:06
3 * * * * /home/meng/hello.sh就是:每小时的03分执行/home/meng/下的hello.sh脚本*/5 9-17 * * mon,tue,wed,thur,fri wall "Are we there yet?"
5 3 * * * /usr/bin/apt-get update 就是天天的03:05执行“/usr/bin/apt-get update
*/5 9-17 * * mon,tue,wed,thur,fri wall "Are we there yet?" 就是在全部的工做日的(9-17点)每过5分钟出现"Are we there Yet?"
#每晚的21:30重启apache。
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#每个月一、十、22日
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#天天早上6点10分
10 6 * * * date
#每两个小时
0 */2 * * * date
#晚上11点到早上8点之间每两个小时,早上8点
0 23-7/2,8 * * * date
#每月的4号和每一个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * mon-wed date
#1月份日早上4点
0 4 1 jan * date
crontrab重启
service cron restart 重启 service cron reload 从新加载配置文件