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 前五 ...
随机推荐
- Spring Bean 的加载过程
Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...
- Administrator 被禁用
Administrator 被禁用 问题: 重装系统后出现输入用户名和密码的情况,原因是Administrator被禁用. 解决方法: 1.开机后shift+重启 –>选择疑难解答 --> ...
- 使用conda部署jupyterhub以及ladp验证的安装
前提:机器安装有conda环境 官方文档:http://jupyterhub.readthedocs.io/en/stable/quickstart.html 1.安装conda3 jupyterhu ...
- windows10 配置SSH连接Github、配置SSH Key
由于你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以,需要设置SSH key. 第1步:创建SSH Key.在用户主目录下[我的电脑是C:\Users\ad],看看有没有.ssh ...
- double中首字母大写与小写的区别
Double 是类 double是基础数据类型.Double类型是double的包装类.Double 和double之间的相互转化称为自动拆箱和自动装箱.如果从对象角度理解,那么Double就是对象, ...
- 如何查看Ubuntu版本
有时候需要查看一下系统安装的Ubuntu的版本,最简单的方式是输入lsb_release -a. whatis lsb_release输出:print distribution-specific in ...
- Cytoscape——实例
本文将具体操作怎样用Cytoscape绘制网络图 Cytoscape所支持的数据格式:1.*.sif格式: nodeA<interaction>nodeB nodeC<inter ...
- Delphi 版本信息获取函数 GetFileVersionInfo、GetFileVersionInfoSize、VerFindFile、VerInstallFile和VerQueryValue
一.版本信息获取函数简介和作用 获取文件版本信息的作用: 1. 避免在新版本的组件上安装旧版本的相同组件: 2. 在多语言系统环境中,操作系统根据文件版本信息里提供的语言信息在启动程序时决定使用的正确 ...
- 树莓派配置freeopcua
一.下载freeopcua的源码 git clone https://github.com/FreeOpcUa/freeopcua.git 切换回2016-10-8的版本 git reset 8c16 ...
- 浅析Draw Call
Draw Call是CPU对GPU的一种命令,仅仅指向一个需要被渲染的图元列表,在OpenGL和DirectX中分别体现为glDrawElements和DrawIndexedPrimitive图像编程 ...