laravel - 使用 laravel 的 cron 作业向用户发送电子邮件
问题描述
请帮助我,我正在尝试在我的服务器上进行 cron 作业以向用户发送电子邮件我创建了这样的命令:
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'contract:end';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send an email to user about the end of a contract';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$contracts = Contract::where('end_date', Carbon::parse(today())->toDateString())->get();
foreach ($contracts as $contract) {
Mail::to(Setting::first()->value('email'))->send(new ContractEmail($contract));
}
}
在我的kernel.php
我添加了以下代码:
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
Commands\ContractEnd::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('contract:end')
->everyMinute();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
在我的服务器上,我运行以下命令来运行 cron 作业:
cd /archive/artisan && schedule:run >> /dev/null 2>&1
但它没有发送任何电子邮件,任何人都可以帮助我,因为这是我第一次使用它?
解决方案
因为您在服务器中的命令是错误的,您需要指定正确的路径,该路径将包含/home/您的用户名/您的网站文件夹/artisan 像这样
/usr/local/bin/php /home/your user name which you can get in from job page/archive/artisan schedule:run 1>> /dev/null 2>&1
推荐阅读
- c# - 如何限制运行的并行任务数量?
- java - 如何确定 JAR 库/依赖项是否与旧的 Android 设备完全兼容?
- ascii - 如何将互联网 URL 中的 ASCII 文件直接写入 Stata?
- c++ - 如何在 Web 上显示 H264 TCP 流
- python - Neovim、JSON、coc-python 片段不工作
- json - 将对象的 json 数组导入 Elastic Search App
- python - Pytorch:需要帮助实现自定义滑动窗口
- php - 致命错误:未捕获的错误:PDOStatement 类的对象无法转换为字符串
- qt - 使用 linuxdeployqt 打包的 Qt 应用程序中未显示图像
- android - 未解析的引用,但可以编译