php - 我无法运行工匠计划:在共享主机上运行 cron 命令
问题描述
我想在我的 laravel 5.8 项目中实现任务调度。为此,我创建了一个自定义工匠命令artisan send: credentials
,该命令根据特定用户的状态向其发送电子邮件。
sendUserCredentials.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Mail\credentialsEmail;
use App\Models\userModel;
use Mail;
class sendUserCredentials extends Command
{
protected $signature = 'send:credentials';
protected $description = 'Credentials send Successfully!';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$users = userModel::select(["email","username","role","id"])->where("credentials","NO")->get();
foreach ($users as $key => $user) {
Mail::to($user->email)->send(new credentialsEmail($user));
userModel::where("id",$user->id)->update(["credentials"=>"SEND"]);
}
}
}
我在 kernel.php 中添加了这个命令,以便我可以使用 laravel 任务调度程序运行这个命令。
内核.php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
Commands\sendUserCredentials::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('send:credentials')
->everyMinute();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
所以在我的本地服务器上,当我运行这个命令时,一切都像一个魅力,php artisan schedule:run
但在共享服务器上,当我使用 cron 命令运行调度程序时,*****/path/to/project/artisan schedule:run >> /dev/null 2>&1
它给了我这样的错误
local.ERROR: The Process class relies on proc_open, which is not available on your PHP installation. {"exception":"[object] (Symfony\\Component\\Process\\Exception\\LogicException(code: 0): The Process class relies on proc_open, which is not available on your PHP installation. at /path/to/vendor/vendor/symfony/process/Process.php:143)
但是当我直接使用 cron 作业运行 artisan 命令时,*****/path/to/project/artisan send:credentials >> /dev/null 2>&1
没有错误并且电子邮件发送成功!
解决方案
我正在使用 laravel 5.8 并将我的网站部署在 namecheap 共享主机上。以下命令帮助我正确执行 cron 作业:
*/5 * * * * /usr/local/bin/php /home/YOUR_USER/public_html/artisan schedule:run >> /home/YOUR_USER/public_html/cronjobs.txt
由于 namecheap 允许至少 5 分钟的间隔,因此上述命令将在 5 分钟后执行,并且输出将显示在文本文件中。
推荐阅读
- azure-data-factory - 根据 ADF 中的特定条件为 csv 文件中的所有列写入零
- node.js - Github 私有包未安装在 docker build 中
- docker - 当我使用 ssh 或 exec/attach 连接到我的 Docker 容器时,为什么 PATH 会有所不同?
- javascript - 如何让我的不和谐机器人在崩溃时自动重启?
- azure-devops - 使用 Azure Devops 授予对 Azure Databricks 的访问权限
- windows - 如何在没有触摸命令的情况下“触摸”原始 CMD 中的文件?
- node.js - Web3 BatchRequest 总是返回未定义,我做错了什么?
- javascript - 为什么我的 html 元素的 id 元素没有被 javascript 正确处理?
- visual-studio-code - Visual Studio 代码中的 Div 颜色
- php - 在 WooCommerce 中隐藏一组基于产品 ID 的运输方式