简体   繁体   中英

Use Laravel schedule to run a controller function in everyMinute

I have tried to use Laravel Scheduler to run a controller function everyMinute but not working

This is my app\\console\\kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $obj = new HomeController();

        $schedule->call(function () use ($obj) {


            $obj->daily_set_profit(); //call method


        })->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

Don't forget to run

php artisan schedule:run

and At last you can manage this command on scheduling task, you have to add a single entry to your server's crontab file

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

or

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

follow this tutorial , simple and very useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM