Yii2 定时任务创建(Console 任务)

栏目: 服务器 · 发布时间: 6年前

内容简介:Yii2的定时任务可以有两种写法,原理都是通过服务器的定时任务去调用1.通过调用指定的URL访问 就相当于在浏览器中访问2.通过console调用

Yii2的定时任务可以有两种写法,原理都是通过服务器的定时任务去调用

1.通过调用指定的URL访问 就相当于在浏览器中访问

2.通过console调用

下面我们就来说说Console 是如何实现定时任务的

一、首先在创建Controlle 并继承 yii\console\Controlle;

<?php
namespace console\controllers;
 
use yii\console\Controller;
 
class TestController extends Controller
{
    public function actionTest()
    {
        echo "test\n";
    }
}

二、测试是否成功

/usr/local/php/bin/php /your_project_path/yii test/test

三、设置定时任务

linux下,运行crontab -e

30 21 * * * /usr/local/php/bin/php /your_project_path/yii test/test

上面的例子表示每晚的21:30执行上面的定时程序

下面是定时任务设置的一些基本介绍

基本格式 :

* * * * * 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。
 
0 */1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小时重启apache

以上所述就是小编给大家介绍的《Yii2 定时任务创建(Console 任务)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Paradigms of Artificial Intelligence Programming

Paradigms of Artificial Intelligence Programming

Peter Norvig / Morgan Kaufmann / 1991-10-01 / USD 77.95

Paradigms of AI Programming is the first text to teach advanced Common Lisp techniques in the context of building major AI systems. By reconstructing authentic, complex AI programs using state-of-the-......一起来看看 《Paradigms of Artificial Intelligence Programming》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具