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. js 数组、字符串、Json互相转换

    arr.join(): 数组转字符串 let arr = [1,2,3,4]; let str = arr.join(','); arr.split():字符串转数组 let str = '1,2,3 ...

  2. 转帖 移动前端开发之viewport的深入理解

    在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或 ...

  3. 0xC0000005: 写入位置 0x00000000 时发生访问冲突的解决办法(转)

    上面的意识就是你吧值付给了不该赋给的变量,或者说你把值付给了不能付给的变量(或者常量) ()最简单也最直接的错误可能就是scanf()的问题,我们都知道输入的时候都是scanf("%格式&q ...

  4. python3.x 匿名函数lambda_扩展sort

    #匿名函数lambda 参数: 表达式关键字 lambda 说明它是一个匿名函数,冒号 : 前面的变量是该匿名函数的参数,冒号后面是函数的返回值,注意这里不需使用 return 关键字. ambda只 ...

  5. STM点滴一

    就就是你用BSRR和BRR去改变管脚状态的时候,没有被中断打断的风险.也就不需要关闭中断. This way, there is no risk that an IRQ occurs between ...

  6. 51单片机PC程序计数器

    PC是一个16位的计数器.用于存放和指示下一条要执行的指令的地址.寻址范围达64KB.PC有自动加1功能,以实现程序的顺序执行.PC没有地址,是不可寻址的,无法用指令对它进行读写.但在执行转移.调用. ...

  7. Android中的ImageView的scaleType属性详解

    ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...

  8. 【已转移】【缓存与性能优化】一篇文章搞掂:Redis

    本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 一.什么是Redis 全称: Remote Dictionary Server 远程字典服务器 实质: 一个缓存结构服务器或数据结 ...

  9. app = Flask(__name__) 是个什么东西

    """第一部分,初始化:所有的Flask都必须创建程序实例, web服务器使用wsgi协议,把客户端所有的请求都转发给这个程序实例 程序实例是Flask的对象,一般情况下 ...

  10. 在 MacBook 上安装 Ubuntu

    建立ubuntu 安裝U盤,加入usb port,按住option key 開機,正常安裝. 完成後,會缺少wifi drive sudo apt-get install bcmwl-kernel-s ...