安排cron工作laravel

时间:2015-07-21 23:36:28

标签: php laravel cron laravel-5

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

我在JOB文件夹

中创建了App/Jobs
<?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中的

我已将此链接添加到schedule方法

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

        })->everyMinute();
    }

请注意我已将everyMinute用于测试目的

crontab -e我添加了

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

但我认为时间表似乎没有运行,因为当我检查results表时,isactive字段没有更改。

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

2 个答案:

答案 0 :(得分:7)

  

我想知道如何安排一个cron作业每天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从命令行运行它并查看输出。

答案 1 :(得分:0)

namespace App\Console\Commands;

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

使用Illuminate \ Console \ Command;

CronJob类扩展Command {     / **      *控制台命令的名称和签名。      *      * @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