首页 > 解决方案 > 我无法运行工匠计划:在共享主机上运行 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没有错误并且电子邮件发送成功!

标签: phplaravel-5cronscheduled-taskslaravel-artisan

解决方案


我正在使用 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 分钟后执行,并且输出将显示在文本文件中。


推荐阅读