如何从linux运行yii2 cronjob

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?

Do this: (take the $ symbol off)

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

run in yii2project folder

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.

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