1.artisan命令:

 php artisan make:command SendRejectEmail

2.app/Console/Commands下就会看到SendRejectEmail.php

 /**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send-reject-email:email'; /**
* The console command description.
*
* @var string
*/
protected $description = 'send reject email';
  • 需要执行的方法写在handle中
     /**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{ }
 <?php
namespace App\Console\Commands; use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Models\ResumeRejectMail;
use App\Mail\RejectMail;
use App\ElasticSearch\Index\ResumeFilterIndex;
use Mail; class SendRejectEmail extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send-reject-email:email'; /**
* The console command description.
*
* @var string
*/
protected $description = 'send reject email'; /**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
} /**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$lists = ResumeRejectMail::Status(ResumeRejectMail::STATUS)->select('id','resume_id','reject_time')->get();
$index = new ResumeFilterIndex(); if($lists){
foreach ($lists as $key=>$val)
{
if(Carbon::now()->timestamp > strtotime($val->reject_time)){
//获取es上当前id信息
$data = $index->getValue($val->resume_id);
if($data){
$toMail = $data['email'];
// 获取邮箱标题
$title = '面试邀请';
Mail::to($toMail)->send(new RejectMail($data,$title)); //更改es简历为失效
$es_data = ['status' => 3];
$index->updateValue($val->resume_id,$es_data); //更新邮件拒信表
ResumeRejectMail::ID($val->id)->update([
'email' => $toMail,
'sendtime' => Carbon::now(),
'status' => 2,
]);
}
}
}
} return true;
}
}

3.在app/Console/Kernel中注册路由

 <?php

 namespace App\Console;

 use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
Commands\SendRejectEmail::class,
]; /**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('send-reject-email:email')->everyMinute();
} /**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands'); require base_path('routes/console.php');
}
}

4.在linux的crontab -e中插入

建议:这里的php路径不要和文档一样就写php,否则crontab可能识别不到,造成手动执行可以生效,写在定时器中却不执行,执行 whereis php 可以查看当前php执行文件,按实际填写php路径

 * * * * * /usr/local/php/bin/php /usr/share/nginx/recruitmentapi/artisan schedule:run >> /dev/null 2>&1

注意:laravel的command需要关闭proc_open、proc_get_status函数禁用。

5.关于laravel crontab不执行的问题

手动执行:php artisan send-reject-email:email 可以生效

但是写在定时器中却不执行 :* * * * * php /usr/share/nginx/recruitmentapi/artisan schedule:run >> /dev/null 2>&1

原因:php的路径并不识别 或者说 crontab 中使用的php的可执行文件 和在脚本中执行的php文件不一样

执行 whereis php

[root@izwz94f1q2m5ldkvsdx5rkz recruitmentapi]# whereis php
php: /usr/local/php /usr/local/php/bin/php

可以发现 当前有不只一个的php执行文件 发现自己在使用的php路径之后 修改 crontab中的配置

 crontab -e

 * * * * * /usr/local/php/bin/php /usr/share/nginx/recruitmentapi/artisan schedule:run >> /dev/null 2>&1

查看crontab日志记录:tail -f /var/log/cron

laravel写crontab定时任务(发送邮件)和laravel crontab不执行的问题-LMLPHP

问题解决

参考:

laravel写crontab: https://www.jianshu.com/p/fc90ff514ce7

laravel crontab不执行的问题: https://blog.csdn.net/qq_36638599/article/details/80692922

05-28 14:06