Schedule cron job laravel

I would like to know how to schedule a cron job to run daily at 00:01.

I created a JOB in the App/Jobs folder

 <?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(); } } 

in application / console / Kernel.php I added this link to the schedule method

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

Please note that I used everyMinute for testing

In crontab -e I added

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

but the graph does not seem to work, I think, because when I check the results table, the isactive field isactive not changed.

I wonder where I am going wrong, please. If someone did it in L5. What am I missing?

+6
source share
1 answer

I would like to know how to schedule a cron job to run daily at 00:01.

So, do you want it to start daily at 00:01?

Answer:

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

Also: (edited in response to your comments)

Here's how I do it:

Command:

 <?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, ]); } } 

Scheduler:

 <?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 '); } } 

If you have done this, you can also run it from the command line using php artisan update-active and see the output.

+6
source

All Articles