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

1.artisan命令:

1 php artisan make:command SendRejectEmail

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

 1 /**
 2  * The name and signature of the console command.
 3  *
 4  * @var string
 5  */
 6 protected $signature = 'send-reject-email:email';
 7 
 8 /**
 9  * The console command description.
10  *
11  * @var string
12  */
13 protected $description = 'send reject email';
  • 需要执行的方法写在handle中
1     /**
2      * Execute the console command.
3      *
4      * @return mixed
5      */
6     public function handle()
7     {
8        
9     }
 1 <?php
 2 namespace AppConsoleCommands;
 3 
 4 use CarbonCarbon;
 5 use IlluminateConsoleCommand;
 6 use AppModelsResumeRejectMail;
 7 use AppMailRejectMail;
 8 use AppElasticSearchIndexResumeFilterIndex;
 9 use Mail;
10 
11 class SendRejectEmail extends Command
12 {
13     /**
14      * The name and signature of the console command.
15      *
16      * @var string
17      */
18     protected $signature = 'send-reject-email:email';
19 
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'send reject email';
26 
27     /**
28      * Create a new command instance.
29      *
30      * @return void
31      */
32     public function __construct()
33     {
34         parent::__construct();
35     }
36 
37     /**
38      * Execute the console command.
39      *
40      * @return mixed
41      */
42     public function handle()
43     {
44         $lists = ResumeRejectMail::Status(ResumeRejectMail::STATUS)->select('id','resume_id','reject_time')->get();
45         $index = new ResumeFilterIndex();
46 
47         if($lists){
48             foreach ($lists as $key=>$val)
49             {
50                 if(Carbon::now()->timestamp > strtotime($val->reject_time)){
51                     //获取es上当前id信息
52                     $data = $index->getValue($val->resume_id);
53                     if($data){
54                         $toMail = $data['email'];
55                         // 获取邮箱标题
56                         $title = '面试邀请';
57                         Mail::to($toMail)->send(new RejectMail($data,$title));
58 
59                         //更改es简历为失效
60                         $es_data =  ['status' => 3];
61                         $index->updateValue($val->resume_id,$es_data);
62 
63                         //更新邮件拒信表
64                         ResumeRejectMail::ID($val->id)->update([
65                             'email' => $toMail,
66                             'sendtime' => Carbon::now(),
67                             'status' => 2,
68                         ]);
69                     }
70                 }
71             }
72         }
73 
74         return true;
75     }
76 }

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

 1 <?php
 2 
 3 namespace AppConsole;
 4 
 5 use IlluminateConsoleSchedulingSchedule;
 6 use IlluminateFoundationConsoleKernel as ConsoleKernel;
 7 
 8 class Kernel extends ConsoleKernel
 9 {
10     /**
11      * The Artisan commands provided by your application.
12      *
13      * @var array
14      */
15     protected $commands = [
16         //
17         CommandsSendRejectEmail::class,
18     ];
19 
20     /**
21      * Define the application's command schedule.
22      *
23      * @param  IlluminateConsoleSchedulingSchedule  $schedule
24      * @return void
25      */
26     protected function schedule(Schedule $schedule)
27     {
28         // $schedule->command('inspire')
29         //          ->hourly();
30         $schedule->command('send-reject-email:email')->everyMinute();
31     }
32 
33     /**
34      * Register the commands for the application.
35      *
36      * @return void
37      */
38     protected function commands()
39     {
40         $this->load(__DIR__.'/Commands');
41 
42         require base_path('routes/console.php');
43     }
44 }

 4.在linux的crontab -e中插入

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

1 * * * * * /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中的配置

1 crontab -e
2 
3 
4 * * * * * /usr/local/php/bin/php /usr/share/nginx/recruitmentapi/artisan schedule:run >> /dev/null 2>&1

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

问题解决

参考:

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

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

原文地址:https://www.cnblogs.com/clubs/p/11301504.html