Thinkphp crontab 定时任务

安装crontab:

网上有很多例子,安装启动服务等。

crontab常用命令:

crontab -e:编辑计时器设置

crontab -l:查看计时器列表

crontab -r:删除计时器设置

crontab -u<用户名>:指定用户名

crontab格式

从网上搜的一些例子

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

 
第1列表示分钟1~59 每分钟用*或者*/1表示
 
第2列表示小时1~23(0表示0点)
 
第3列表示日期1~31
 
第4列 表示月份1~12
 
第5列标识号星期0~6(0表示星期天)
 
第6列要运行的命令
 
 
 
# Use the hash sign to prefix a comment
 
# +—————- minute (0 – 59)
 
# | +————- hour (0 – 23)
 
# | | +———- day of month (1 – 31)
 
# | | | +——- month (1 – 12)
 
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
 
# | | | | |
 
# * * * * * command to be executed
30 21 * * * /etc/init.d/nginx restart
 
每晚的21:30重启nginx。
 
 
 
45 4 1,10,22 * * /etc/init.d/nginx restart
 
每月1、10、22日的4 : 45重启nginx。
 
 
 
10 1 * * 6,0 /etc/init.d/nginx restart
 
每周六、周日的1 : 10重启nginx。
 
 
 
0,30 18-23 * * * /etc/init.d/nginx restart
 
每天18 : 00至23 : 00之间每隔30分钟重启nginx。
 
 
 
0 23 * * 6 /etc/init.d/nginx restart
 
每星期六的11 : 00 pm重启nginx。
 
 
 
* */1 * * * /etc/init.d/nginx restart
 
每一小时重启nginx
 
 
 
* 23-7/1 * * * /etc/init.d/nginx restart
 
晚上11点到早上7点之间,每 隔一小时重启nginx
 
 
 
0 11 4 * mon-wed /etc/init.d/nginx restart
 
每月的4号与每周一到周三 的11点重启nginx
 
 
 
0 4 1 jan * /etc/init.d/nginx restart
 
一月一号的4点重启nginx
 
 
 
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
 
 
每半小时同步一下时间

 

添加定时任务:

thinkphp可以在终端中直接用php执行根目录的index.php文件,后边可追加参数

先在终端测试是否可行

$ php /home/vagrant/Code/www/index.php Home/Index/test

结果报错:

PHP Warning:  require(./ThinkPHP/ThinkPHP.php): failed to open stream: No such file or directory in /home/vagrant/Code/www/index.php on line 26

Warning: require(./ThinkPHP/ThinkPHP.php): failed to open stream: No such file or directory in /home/vagrant/Code/www/index.php on line 26
PHP Fatal error:  require(): Failed opening required './ThinkPHP/ThinkPHP.php' (include_path='.:/usr/share/php') in /home/vagrant/Code/www/index.php on line 26

Fatal error: require(): Failed opening required './ThinkPHP/ThinkPHP.php' (include_path='.:/usr/share/php') in /home/vagrant/Code/www/index.php on line 26

修改index.php 改为绝对路径

<?php
// 检测PHP环境
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    die('require PHP > 5.3.0 !');
}

define('APP_ROOT', dirname(__FILE__));

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG', true);

// 定义应用目录
define('APP_PATH', APP_ROOT.'/Application/');

// 引入ThinkPHP入口文件
require APP_ROOT.'/ThinkPHP/ThinkPHP.php';

// 亲^_^ 后面不需要任何代码了 就是如此简单
?>

再次执行php 成功。

 

终端中执行crontab -e,添加定时任务(第一次会让选择编辑器,以后也可用“select-editor”修改)

* * * * * php /home/vagrant/Code/www/index.php Home/Index/test

发现没有执行,将php也修改为绝对路径

* * * * * /usr/local/php/bin/php -f /home/vagrant/Code/www/index.php Home/Index/test

 

转载于:https://my.oschina.net/FelixZone/blog/1518658

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值