user3374995
user3374995

Reputation: 121

How to run yii2 cronjob from linux

I created a cronjob controller in yii2project/console/controllers :

namespace console\controllers;

use yii\base\Model;

use yii\console\Controller;

use Yii;

class MycronController extends Controller {

    public function actionIndex() {

        echo "cron service runnning";
        die;
    }

}

In windows i am running this file :

D:\xampp\htdocs\yii2project>d:\xampp\php\php yii mycron

output:

cron service running

Now how to run this in linux?

Upvotes: 2

Views: 3501

Answers (4)

The Cron-Job is for CentOs. Open the terminal and navigate to your project folder

cd /var/www/html/advancedyii2

After entering into the folder type

crontab -e

The cron-manager file will be opened in the terminal.

Now edit the cron file like

* * * * * php /var/www/html/advancedyii2/yii controller/function

Before executing the above command make sure a controller is created under

/var/www/html/advancedyii2/console - Here create a controller and function to server your need

For more about Cron-Configurations Visit this link

Upvotes: 0

Gogol
Gogol

Reputation: 3072

None of the solutions worked for me, atleast. To make it work from crontab, you have to provide the following command:

* * * * *  php /path/to/project/root/yii controller-name/action-name

This example will run every minute by the way. For more information about cronjobs, check this link out.

By the way, if you just want to run the job from your SSH terminal, use this one instead:

php /path/to/project/root/yii controller-name/action-name

Edit: Make sure you have run init command after installing yii with composer. That sets the necessary permissions to run the yii script. If you still can't run it, try chmod +x yii to make the script executable.

Upvotes: 1

Thao Ngo
Thao Ngo

Reputation: 3771

php yii mycron

run in yii2project folder

Upvotes: 1

Arturs Smirnovs
Arturs Smirnovs

Reputation: 193

Do this: (take the $ symbol off)

$ /xampp/php/php/./yii mycron

Upvotes: 0

Related Questions