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: https://www.jianshu.com/p/fc90ff514ce7

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

laravel写crontab定时任务(发送邮件)和laravel crontab不执行的问题的更多相关文章

  1. Linux下Crontab定时任务的使用教程 以及 无法执行定时任务的解决方案

     前言 本文学习思路:Linux的corntab定时任务的使用教程  --> 定时任务无效的解决方案  Linux的corntab定时任务的使用教程 1. 首先,输入命令 打开crontab定时 ...

  2. linux crontab定时任务运行shell脚本(shell执行sql文件)

    https://www.cnblogs.com/tiankongjava/p/6106743.html 今天做个linux定时任务(每晚12点把表汇总). 顺便写个博客记录一下~~ 为什么用linux ...

  3. linux crontab 定时任务,任务命令单独linux执行正常,放定时任务就不执行了,解决办法 (原)

    这是我crontab里面的内容 */30 * * * *  ./usr/bin/wget -q -O sync_log.txt http://fly.dllm.cn/index.php/Home/In ...

  4. Linux命令之Crontab定时任务,利用Crontab定时执行spark任务

    Linux命令之Crontab定时任务,利用Crontab定时执行spark任务 一.Linux命令之Crontab定时任务 1.1 常见Crontab任务 1.1.1 安装crontab 1.1.2 ...

  5. crontab定时任务配置记录

    一.前言 今天简单记录下crontab的配置 二.crontab目录 /etc/crontab 文件 这是系统运行的调度任务 /var/spool/cron 目录 用户自定义的crontab任务放在此 ...

  6. 【Linux】crontab 定时任务

    7月份,公司海外运营国发生数据库联接未释放,造成连接池连接不足的情况, 当时查询并没有及时解决问题, 为了避免现场同事多次人工重启系统的,因此写了个shell脚本,通过crontab 实现系统重启,但 ...

  7. Linux crontab 定时任务命令详解

    一.简介 crontab 命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 crontab 文件中,以供之后读取和执行.通常,crontab 储存的指令被守护进程激活, cr ...

  8. Linux crontab 定时任务

    http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html 19. crontab 定时任务 通过crontab 命令,我们 ...

  9. crontab 定时任务格式

    如下内容节选自<Linux Crontab 定时任务 命令详解> 用crontab -e 添加要执行的命令 添加的命令必须以如下格式: * * * * * /command path 前五 ...

随机推荐

  1. GOOSE报文解析

    GOOSE报文解析 变电站 使用 MMS报文 http://www.360doc.com/content/16/1014/20/36538220_598459873.shtml

  2. bi包

    闭包是函数式编程的概念 闭包的定义:由函数极其封闭的自由变量组成的集合体. 其实每一个函数都是一个闭包 特点:函数中嵌套函数. 在返回的时候不仅返回函数本身,而且返回内部函数的值,关键词‘释放’ 闭包 ...

  3. oracle trim无效?

    这里说说如果是全角空格怎么去除 方法一 trim(TO_SINGLE_BYTE('aaa')) 方法二 SELECT TRIM(replace('aaa',' ','')) FROM dual

  4. Mysql 数据库默认值选 ''" 、Null和Empty String的区别

    两者的查询方式不一样:NULL值查询使用is null/is not null查询,而empty string可以使用=或者!=.<.>等算术运算符,这点算是最主要的区别了. 对于myis ...

  5. 常用Concurrent.util包工具类——高并发

    一 Concurrent.util常用类: 1. CyclicBarrier: 假设有场景:每个线程代表一个跑步运动员,当运动员都准备好后,才一起出发只要有一个人没有准备好,大家都等待. import ...

  6. 【Flutter学习】页面布局之其它布局处理

    一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...

  7. element-ui弹窗实现自定义宽度

    <el-dialog :title="title" :visible.sync="hiddenDialogCommon" :width="wid ...

  8. jquery实现简单的搜索功能

    管理系统中需要实现导航列表的搜索功能,写了一个简单的小栗子: <!DOCTYPE html> <html lang="en"> <head> & ...

  9. tp5 thinkphp 使用phpqrcode生成带Logo的二维码

    1 下载生成二维码类库 composer require aferrandini/phpqrcode 2 点击按钮下载 //二维码下载 public function down_qrcode() { ...

  10. Java 从入门到进阶之路(十七)

    在之前的文章我们介绍了一下 Java 中类的内部类,本章我们来看一下 Java 中的正则表达式. 在任何一种语言中,都绕不开正则表达式,而且大部分语言的正则表达式都有预定义的字符集,且预定义的字符集也 ...