为什么 Laravel Cron Job 无法在 cpanel 上运行?

问题描述 投票:0回答:3

我正在尝试通过 cron 作业在 cpanel 上运行计划运行命令,但它不起作用。我使用以下命令:

*   *   *   *   *   /usr/local/bin/ea-php73 /home/mozeshan/xicov/artisan schedule:run >> /dev/null 2>&1

注意:我的公共文件位于 public_html 文件夹中,其余文件位于 /home/mozeshan/xicov 中

我的命令文件如下所示:

class MakeWinners extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:winners';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Make winners daily and weekly';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $date = Carbon::now()->format('Y-m-d');
        // current day
        $day = Carbon::now()->format('D');
        // check for weeekly winner
        if($day === "Mon"){
            $weekly = PurchaseTicket::whereBetween('created_at',array(\Carbon\Carbon::now()->startOfWeek(),\Carbon\Carbon::now()->endOfWeek()))->select('user_id', DB::raw('COUNT(*) AS cnt'))
            ->groupBy('user_id')
            ->orderByRaw('COUNT(*) DESC')
            ->get();
            $weeklyCriteria = WeeklyWinnerCriteria::findOrFail(1);
            $max = 0;
            // getting weekly winner
            foreach($weekly as  $k => $v){
                $max = max( array( $max, $v['cnt'] ) );
                if($max === $v->cnt){
                    $weeklyWinner = new WeeklyWinner();
                    $weeklyWinner->amount = $weeklyCriteria->amount;
                    $weeklyWinner->user_id = $v->user_id;
                    $weeklyWinner->save();
                }
            }

            // for Daily winner
            $ticketsPurchasers = PurchaseTicket::where('created_at','like',"%".$date."%")->get();
            $criteria = SelectWinner::findOrFail(1);
            $endPoint =$ticketsPurchasers->count();    
            for($i = 0; $i < $criteria->winners; $i++)
            {
                $winnerIndex =rand(0,$endPoint-1);
                $winner = $ticketsPurchasers[$winnerIndex];
                $PreviousWinner = new PreviousWinner();
                $PreviousWinner->ticket_number = $winner->ticket_number;
                $PreviousWinner->amount = $criteria->amount;
                $PreviousWinner->user_id = $winner->user_id;
                $PreviousWinner->save();
            }

        }else{

            // for daily winners if day is not monday
            // $ticketsPurchasers = PurchaseTicket::where('created_at','like',"%".$date."%")->groupBy('ticket_id')->get();
    
            // foreach($ticketsPurchasers as $ticketsPurchaser){
            //     $criteria = Ticket::findOrFail($ticketsPurchaser->ticket_id);
            //     $purchased = PurchaseTicket::whereDate('created_at',$date)->where('ticket_id',$ticketsPurchaser->ticket_id)->get();
            //     for($i = 0; $i < $criteria->winner; $i++){
            //         $endPoint =$purchased->count();
            //         $winnerIndex =rand(0,$endPoint-1);
            //         $winner = $purchased[$winnerIndex];
            //         $PreviousWinner = new PreviousWinner();
            //         $PreviousWinner->ticket_number = $winner->ticket_number;
            //         $PreviousWinner->amount = $criteria->amount;
            //         $PreviousWinner->user_id = $winner->user_id;
            //         $PreviousWinner->save();
            //     }
            // }

            $ticketsPurchasers = PurchaseTicket::where('created_at','like',"%".$date."%")->get();
            $criteria = SelectWinner::findOrFail(1);
            $endPoint =$ticketsPurchasers->count();    
            for($i = 0; $i < $criteria->winners; $i++)
            {
                $winnerIndex =rand(0,$endPoint-1);
                $winner = $ticketsPurchasers[$winnerIndex];
                $PreviousWinner = new PreviousWinner();
                $PreviousWinner->ticket_number = $winner->ticket_number;
                $PreviousWinner->amount = $criteria->amount;
                $PreviousWinner->user_id = $winner->user_id;
                $PreviousWinner->save();
            }
            
        }
    }
}

在 Kernel.php 中:

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\MakeWinners::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('make:winners')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

提前致谢。

php html laravel command scheduler
3个回答
1
投票
  1. 确保您的命令已注册 app/Console/Kernel.php
    protected $commands = [
        MakeWinners::class,
    ];

您应该咨询您的托管提供商并询问他们有关添加 cronjob 的信息,因为该过程可能略有不同。

这是我的面板所说的:

General example:
/usr/local/bin/php /home/thinkpro/public_html/path/to/cron/script

Domain-specific example:
/usr/local/bin/ea-php99 /home/thinkpro/domain_path/path/to/cron/script
In the above example, replace “ea-php99” with the PHP version assigned to the domain you wish to use. Look in the MultiPHP Manager for the actual PHP version assigned to a domain.

这是我的托管提供商在支持请求中所说的:

As my colleague, Rohan suggested that it might not be possible to run php artisan commands via cron jobs because the environment variables are different for the cron scheduler.

However, if you are looking for a set path then you can use the cd command combined with the cron job and give it a try.

Here is an example of a cron schedule for WordPress websites to run their self-made "wp-cron.php" script.
cd /home/your_username/public_html; php -q wp-cron.php

这是我如何通过basezap 托管在我的cpanel 中运行调度程序的示例。附截图

cd /home/thinkpro/mina.quillweb.life && php artisan schedule:run >> /dev/null 2>&1

我相信你应该尝试以下方法

*   *   *   *   *   /usr/local/bin/ea-php73 /home/mozeshan/xicov && php artisan schedule:run >> /dev/null 2>&1


1
投票

我解决了这个问题,我认为问题出在 php 的版本上。这个命令对我有用:

*   *   *   *   *   /usr/local/bin/php /home/unialso1/xicov.unialsolutions.com/artisan schedule:run

0
投票

在 Cpanel 中,这个命令对我有用:

/usr/local/bin/php /home/hosting_user/public_html/artisan schedule:run >> /dev/null 2>&1
© www.soinside.com 2019 - 2024. All rights reserved.