繁体   English   中英

为什么 Laravel Cron Job 不在 cpanel 上工作?

[英]Why Laravel Cron Job is not working on cpanel?

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

*   *   *   *   *   /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');
    }
}

提前致谢。

  1. 确保您的命令已注册 app/Console/Kernel.php
    protected $commands = [
        MakeWinners::class,
    ];

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

这是我的 cpanel 所说的:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM