Linux 定时任务 — Crontab

173次阅读
没有评论

一、Crontab 是什么?

Crontab 是一个用于设置周期性被执行的任务的工具 , 类似我们生活中的闹钟.

被周期性执行的任务我们称之为 Cron Job
周期性执行的任务列表我们称之为 Cron Table

二、Crontab 前期准备

1、Crontab 前期准备, 检查是否安装 Cron 服务

crontab –l 查看 crontab 是否安装

service crond status 检查 crontab 运行状态

2、若为安装使用 yum 进行安装
  yum –y install vixie-cron
  yum  -y install  crontabs

三、Crontab 指令

crontab  [-u username] 选项

-e (编辑工作表)

 -l (列出工作表里的命令)

 -r (删除工作作)

四、Crontab 结构构成

1、crontab 的命令构成为时间 + 动作,其时间有分、时、日、月、周五种;

例:* * * * * myCommand

2、时间操作符
①* 取值范围内的所有数字
②/ 每过多少个数字
③- 连续数字
④,散列数字

五、Crontab 时间事例

①* * * * * myCommand
②3,15 * * * * myCommand
③3,15 8-11 * * * myCommand
④3,15 8-11 */2  *  * myCommand
⑤3,15 8-11 * * 1 myCommand
⑥30 21 * * * /etc/init.d/smb restart
⑦45 4 1,10,22 * * /etc/init.d/smb restart
⑧10 1 * * 6,0 /etc/init.d/smb restart
⑨0,30 18-23 * * * /etc/init.d/smb restart
⑩0 23 * * 6 /etc/init.d/smb restart
11* */1 * * * /etc/init.d/smb restart
12*  23-7/1 * * * /etc/init.d/smb restart

六、Crontab 执行项目文件

1、执行 php 文件
①以 php 执行程序执行 php 文件

  * * * * * /bin/phpindex.php

②以 curl 形式执行

  * * * * * curl –I  http://newshop.com/module/controller/action

七、Crontab 文件存放

1、/var/spool/cron/ 目录下存放的是每个用户包括 root 的 crontab 任务,每个任务以创建者的名字命名
2、/etc/crontab 文件负责调度各种管理和维护任务。
3、/etc/cron.d/ 目录用来存放任何要执行的 crontab 文件或脚本。
4、还可以把脚本放在 /etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly 目录中,让它每小时 / 天 / 星期、月执行一次。

正文完
有偿技术支持加微信
post-qrcode
 
评论(没有评论)
验证码