每个周末都要运行Laravel时间表

问题描述 投票:-1回答:3

任何人都可以告诉我,我需要在Laravel 5.4中创建一个自动每周末运行的时间表。

首先,我在/app/Console/Commands/创建了一个文件FollowAdvisor.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class FollowAdvisor extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'follow:advisor';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        DB::table('followers')->delete();
        $this->info('All inactive users are deleted successfully');
    }
}

然后我在位于/api/app/Console/的Kernel.php上访问此文件

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use DB;
use App\Model\Follow;

class Kernel extends ConsoleKernel
{

    protected $commands = [
            Commands\FollowAdvisor::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->exec('follow:advisor')->everyMinute();

    }

    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

当我在命令提示符下运行命令php artisan follow:advisor时。这是工作。但我希望它必须自动运行。

谢谢

laravel laravel-5 laravel-5.4
3个回答
6
投票

https://laravel.com/docs/5.4/scheduling

  1. 确保运行的crontab指向Laravel文件artisan * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
  2. ->saturdays();每个星期六都是一个比crontab更简洁/更具表现力的方式

2
投票

启动调度程序使用调度程序时,只需将以下Cron条目添加到服务器即可。如果您不知道如何将Cron条目添加到服务器

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

这个Cron每分钟都会调用Laravel命令调度程序。执行schedule:run命令时,Laravel将评估您的计划任务并运行到期的任务。


REF:https://laravel.com/docs/5.4/scheduling#introduction


0
投票

在laravel 5.4中创建命令和调度程序后,请在服务器中设置cron。所以你的命令可以自动运行。

以下是每分钟运行的crontab命令。

  • php / path-to-your-project / artisan follow:advisor >> / dev / null 2>&1

我希望它对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.