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(CarbonCarbon::now()->startOfWeek(),CarbonCarbon::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 = [
//
CommandsMakeWinners::class
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $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.
2
Answers
I resolved the issue I think the problem was with the version of php. This command worked for me:
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:
Here is what my hosting provider said in the support request:
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