安排 cron 作业 laravel

Schedule cron job laravel

我想知道如何安排 cron 作业到 运行 每天 00:01。

我在 App/Jobs 文件夹

中创建了 JOB
<?php
namespace App\Jobs;
use App\Models\Result;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use DB;

set_time_limit(0);

class UpdateActive extends Job implements SelfHandling
{
    public static function ActiveUpdate()
    {
        Result::update(['draw_id' => 1, 
                        'isactive' => 0
                       ]);
     }

   public static function downGrade()
   {
   try {
        UserRole::update(['permission' => 1,
                         'isactive' => 2    
        ]);
   } catch (QueryException $e ) {
     //handle error
   }
   }

   public static function handle() 
   {
     self::ActiveUpdate();
     self::downGrade();
   }
 }

在 App/Console/Kernel.php 我已经将这个 link 添加到 schedule 方法

protected function schedule(Schedule $schedule)
    {
        /*$schedule->command('inspire')
                 ->hourly(); */
        $schedule->call(function () {
            $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();

        })->everyMinute();
    }

请注意,我将每一分钟用于测试目的

crontab -e然后我添加了

* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1

但时间表似乎没有 运行 我认为是因为当我检查我的 results table 时 isactive 字段没有改变。

我想知道我哪里出错了。如果有人在 L5 中这样做过。我错过了什么?

I will like to know how to schedule a cron job to run everyday at 00:01.

所以您希望它每天 运行 00:01?

答案:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
    })->dailyAt("00:01");
}

旁白:(根据您的评论进行了编辑)

我会这样做:

命令:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdateActiveCommand extends Command
{
    protected $signature = 'update-active';

    protected $description = 'Update something?';

    public function handle()
    {
        try {
            $this->comment("Update active...");
            $this->updateActive();

            $this->comment("Downgrade...");
            $this->downGrade();

            $this->info("Done!");
        } catch (QueryException $e ) {
            $this->error($e->getMessage());
        }
    }

     private function updateActive()
     {
        Result::update([
            'draw_id'  => 1, 
            'isactive' => 0,
        ]);
     }

     private function downGrade()
     {
        UserRole::update([
            'permission' => 1,
            'isactive'   => 2,
        ]);
     }
}

调度程序:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    protected $commands = [
        \App\Console\Commands\UpdateActiveCommand::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('update-active')
                 ->dailyAt('00:01')
                 ->sendOutputTo(storage_path('logs/update-active.log'))
                 ->emailOutputTo('baako@baako.com');
    }
}

如果你这样做,你也可以从命令行 运行 使用 php artisan update-active 并查看输出。

namespace App\Console\Commands;

使用App\Models\Users; 使用数据库;

使用Illuminate\Console\Command;

class CronJob 扩展命令 { /** * 控制台命令的名称和签名。 * * @var 字符串 */ 受保护的 $signature = 'command:name';

protected $new = 'cronjob';


/**
 * 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()
{
    $users = DB::table('users')
                ->where([
                    ['end_date', '>=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
                    ['start_date', '<=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
                ])->get(); 
    foreach ($users as $values ){

       //Do Something
    }
}

} Laravel cron