关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

Crontab定时任务安装使用详细说明

发布时间:2019-09-27 11:17:10

  crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。通常,crontab储存的指令被守护进程激活。crond 常常在后台运行,每一分钟检查是否有预定的作业需要执行。这类作业一般称为cron jobs。

一、安装

yum -y install vixie-cron
yum -y install crontabs

说明:

vixie-cron 软件包是 cron 的主程序;

crontabs 软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。

二、配置

centos6cron 是 linux 的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:

service crond start     //启动服务
service crond stop      //关闭服务
service crond restart   //重启服务
service crond reload    //重新载入配置
service crond status    //查看crontab服务状态

centos7

systemctl start  crond    //启动服务
systemctl stop crond      //关闭服务
systemctl restart crond   //重启服务
systemctl enable crond    //加入系统启动

在CentOS系统中加入开机自动启动: 

chkconfig --level 345 crond on

crontab直观图示

1518045145742119.png

crontab例子

每隔1小时 2小时的执行job写法

加任务:
  crontab -e
  0 */1 * * * command
  0 */2 * * * command
查询任务是否加了:
  crontab -l
  0 */1 * * * command
  0 */2 * * * command


crontab基本格式 :
*  *  *  *  *  command
分 时 日 月 周 命令

第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

crontab文件的一些例子:
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



/template/Home/Zkeys/PC/Static