安排 cron 作业 laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31551456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Schedule cron job laravel
提问by Baako
I will like to know how to schedule a cron job to run everyday at 00:01.
我想知道如何安排一个 cron 作业在每天 00:01 运行。
I have created JOB
in App/Jobs
folder
我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();
}
}
in App/Console/Kernel.php I have added this link to the schedule
method
在 App/Console/Kernel.php 我已将此链接添加到该schedule
方法
protected function schedule(Schedule $schedule)
{
/*$schedule->command('inspire')
->hourly(); */
$schedule->call(function () {
$check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
})->everyMinute();
}
Please note I have used everyMinute for test purpose
请注意,我已将 everyMinute 用于测试目的
In crontab -e
I then added
在crontab -e
我然后添加
* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1
* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1
but the schedule doesn't seem to run i think because when i check my results
table the isactive
field hasn't changed.
但我认为时间表似乎没有运行,因为当我检查我的results
表时,该isactive
字段没有改变。
I am wondering where I am going wrong please. If anyone has done this in L5. What am I missing?
我想知道我哪里出错了。如果有人在 L5 中这样做过。我错过了什么?
回答by ryanwinchester
I will like to know how to schedule a cron job to run everyday at 00:01.
我想知道如何安排一个 cron 作业在每天 00:01 运行。
So you want it to run daily at 00:01?
所以你想让它每天在 00:01 运行?
Answer:
回答:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
})->dailyAt("00:01");
}
Aside: (edited in response to your comments)
旁白:(根据您的评论进行编辑)
This is how I would do it:
这就是我将如何做到的:
The 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,
]);
}
}
The 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('[email protected]');
}
}
If you did it this way you could also run it from the command line with php artisan update-active
and see the output.
如果你这样做,你也可以从命令行运行它php artisan update-active
并查看输出。
回答by Vishnu B
namespace App\Console\Commands;
use App\Models\Users; use DB;
使用 App\Models\Users; 使用数据库;
use Illuminate\Console\Command;
使用 Illuminate\Console\Command;
class CronJob extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name';
class CronJob extends Command { /** * 控制台命令的名称和签名。* * @var string */ protected $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
}
}