laravel写crontab定时任务(发送邮件)和laravel crontab不执行的问题
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()
{ }
- 附上完整个人代码(laravel发送邮件相关配置可参考我另一篇博客:https://www.cnblogs.com/clubs/p/10640682.html)
<?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不执行的问题的更多相关文章
- Linux下Crontab定时任务的使用教程 以及 无法执行定时任务的解决方案
前言 本文学习思路:Linux的corntab定时任务的使用教程 --> 定时任务无效的解决方案 Linux的corntab定时任务的使用教程 1. 首先,输入命令 打开crontab定时 ...
- linux crontab定时任务运行shell脚本(shell执行sql文件)
https://www.cnblogs.com/tiankongjava/p/6106743.html 今天做个linux定时任务(每晚12点把表汇总). 顺便写个博客记录一下~~ 为什么用linux ...
- linux crontab 定时任务,任务命令单独linux执行正常,放定时任务就不执行了,解决办法 (原)
这是我crontab里面的内容 */30 * * * * ./usr/bin/wget -q -O sync_log.txt http://fly.dllm.cn/index.php/Home/In ...
- Linux命令之Crontab定时任务,利用Crontab定时执行spark任务
Linux命令之Crontab定时任务,利用Crontab定时执行spark任务 一.Linux命令之Crontab定时任务 1.1 常见Crontab任务 1.1.1 安装crontab 1.1.2 ...
- crontab定时任务配置记录
一.前言 今天简单记录下crontab的配置 二.crontab目录 /etc/crontab 文件 这是系统运行的调度任务 /var/spool/cron 目录 用户自定义的crontab任务放在此 ...
- 【Linux】crontab 定时任务
7月份,公司海外运营国发生数据库联接未释放,造成连接池连接不足的情况, 当时查询并没有及时解决问题, 为了避免现场同事多次人工重启系统的,因此写了个shell脚本,通过crontab 实现系统重启,但 ...
- Linux crontab 定时任务命令详解
一.简介 crontab 命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 crontab 文件中,以供之后读取和执行.通常,crontab 储存的指令被守护进程激活, cr ...
- Linux crontab 定时任务
http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html 19. crontab 定时任务 通过crontab 命令,我们 ...
- crontab 定时任务格式
如下内容节选自<Linux Crontab 定时任务 命令详解> 用crontab -e 添加要执行的命令 添加的命令必须以如下格式: * * * * * /command path 前五 ...
随机推荐
- window杀死端口
获取端口的pid:netstat -aon|findstr "8382" 杀死pid : taskkill /pid [] -t -f
- ubuntu openssl 生成密钥对
一般情况下ubuntu和mac系统都会自带openssl,安装之前先测试一下,打开终端,输入openssl,如果出现以下画面,即已安装. root@jiang:/home/kevin/work/ope ...
- VSCode中行数与代码之间用点点点代替
在settings.json中添加一行 "editor.renderWhitespace": "all"
- 【leetcode】931. Minimum Falling Path Sum
题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...
- mybatis plus generator工具集成(一)
参数配置文档 配置分两步 1.添加依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId> ...
- 微信小程序学习笔记(二)--框架-全局及页面配置
描述和功能 框架提供了自己的视图层描述语言 WXML 和 WXSS,以及基于 JavaScript 的逻辑层框架,并在视图层与逻辑层间提供了数据传输和事件系统,让开发者能够专注于数据与逻辑. 响应的数 ...
- Android数字签名的学习(转)
转载地址:http://www.cnblogs.com/feisky/archive/2010/01/17/1650076.html 在Android系统中,所有安装到系统的应用程序都必有一个数字证书 ...
- (转)图文详解MyEclipse中新建Maven webapp项目的步骤
转:https://blog.csdn.net/ClementAD/article/details/46592557 MyEclipse中创建新的Maven项目(webapp目录结构)过程如下: ...
- MCS-51系列单片机和MCS-52系列单片机有何异同
MSC-51:1,片内4K字节程序存储器:2,片内128字节数据存储器:3,片内2个16位硬件定时器/计数器.MSC-52: 1,片内8K字节程序存储器:2,片内256字节数据存储器:3,片内3个16 ...
- selenium多表单操作与多窗口,以及警告框处理
知识是需要经常温习的,不然是很容易遗忘的. 以前自己操作IFRAME,多窗口的时候,觉得很简单.半年没有操作自动化了,知识又还了回去. 写博客有一个好处,可以把自己记住的知识点记录下来,这样,以后自己 ...