php + crontab 执行定时任务

1.yii2中的console

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace appcommands;

use yiiconsoleController;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class HelloController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     */
    public function actionIndex($message = 'hello world')
    {
        echo $message . "
";
    }
}

2.用命令行cmd测试是否成功

(一定要看好路径去执行   yii  这个文件              一定要写绝对路径)

 /你的文件夹路径/yii      你的文件夹路径/console/test(控制器)/index(方法)

 3.设置定时任务

linux下,运行crontab -e

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

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

4.重启crontab

service crond restart

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

基本格式 :

* * * * * 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

参考1:http://www.cnblogs.com/dwj97/p/6632848.html

参考2:https://www.helloweba.com/view-blog-419.html

 
原文地址:https://www.cnblogs.com/wuheng1991/p/7665965.html