Yii 2.0: How to implement cron in Yii 2

6 followers

Create console application

In advance template there is already a file yii. And there is no need to run it as php, it is Linux script.

Create cron service command

Create a controller in console/controllers

I have created as TestController.php

<?php
 
namespace console\controllers;
 
use yii\console\Controller;
 
/**
 * Test controller
 */
class TestController extends Controller {
 
    public function actionIndex() {
        echo "cron service runnning";
    }
 
    public function actionMail($to) {
        echo "Sending mail to " . $to;
    }
 
}

This controller should be use the console controller name space

use yii\console\Controller;

How to run it

run it as

yii test

I have test it on windows by running

D:\xampp\htdocs\yii2>d:\xampp\php\php yii test
cron service runnning
D:\xampp\htdocs\yii2>

How to pass params

yii test/mail [--to="hemctest@gmail.com"]

in windows for test it run as.

D:\xampp\htdocs\yii2>d:\xampp\php\php yii test/mail [--to="hemctest@gmail.com"]
Sending mail to [--to=hemctest@gmail.com]

Official docs for console applications can be seen here.

Total 7 comments

#17192 report it
hemc at 2014/05/12 01:40am
Re- command description

Thanks Shahram. I have downloaded the zip files for both basic and advanced template and i can see the class description when i run "yii". Please check if you are using latest files for advanced template.

#17185 report it
Shahram at 2014/05/10 04:13pm
command description

Thanks for writing.

I tried it, It is working fine, but when I use "yii" command, I can not see the class description in advanced template. In basic template I can see PHPDOC but not in advanced one.

#16985 report it
Ahamed Rifaideen at 2014/04/18 05:40am
No Console Access

sorry currently no console access

#16984 report it
hemc at 2014/04/18 05:30am
How to Use Console in Hosted Server

@ Ahamed Rifaideen Do you have access to console? If not contact to your hosting provider. Once you have access edit the crontab file as in ubuntu i use

sudo crontab -e

and add the command to run files in it. ex. to run a command on every 2 hour i have added

0 */2 * * * ~/sites/www/yii2/yii test
#16983 report it
Ahamed Rifaideen at 2014/04/18 05:17am
How to Use Console in Hosted Server

everything is ok but how do i run a console application on live website

#16977 report it
hemc at 2014/04/17 10:30am
can use models in 1.x also

Thanks. In Yii 1.x also, you can use models. We are using models in 1.1 boilerplate by importing models in console/config/main.php

#16976 report it
HarikrishnanP at 2014/04/17 10:23am
Really helpful.

before I used normal php program as cron. I could't use models so I had to write lot of sql queries. It seems cool!

Leave a comment

Please to leave your comment.

Write new article
  • Written by: hemc
  • Category: How-tos
  • Yii Version: 2.0
  • Votes: +6
  • Viewed: 3,526 times
  • Created on: Apr 17, 2014
  • Last updated: never
  • Tags: cron, yii2, console