Rafif
Rafif

Reputation: 11

Why Laravel Cron Job is not working on cpanel?

I am trying to run schedule run command on cpanel via cron job but its not working. I used following command:

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

Note:my public files are in public_html folder and rest of them are in /home/mozeshan/xicov

My Command file looks like this:

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();
            }
            
        }
    }
}

And in 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');
    }
}

Thanks in advance.

Upvotes: 0

Views: 3336

Answers (3)

O Thạnh Ldt
O Thạnh Ldt

Reputation: 1221

In Cpanel, this cmd working for me:

/usr/local/bin/php /home/hosting_user/public_html/artisan schedule:run >> /dev/null 2>&1

Upvotes: 0

Rafif
Rafif

Reputation: 11

I resolved the issue I think the problem was with the version of php. This command worked for me:

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

Upvotes: 1

MacTavish
MacTavish

Reputation: 59

  1. Make sure that your command is registered app/Console/Kernel.php
    protected $commands = [
        MakeWinners::class,
    ];

You should consult your hosting provider and ask them about adding a cronjob as the process can be a slightly different.

Here is what my cpanel says:

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.

Here is what my hosting provider said in the support request:

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

Here is an example of how I am running the scheduler in my cpanel by basezap hosting. Screenshot attached

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

I believe you should try the following

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

Upvotes: 1

Related Questions