Header Ads

Header ADS

Laravel Cron Job Task Scheduling Run Local And Live server on cpanel

 Step 1: install laravel project via command 

composer create-project --prefer-dist laravel/laravel blog


Step 2:Create new custom artisan command

php artisan make:command DemoCron --command=demo:cron


Step 3: Now make some changes on Command file.

app/Console/Commands/DemoCron.php


<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class DemoCron extends Command

{

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'demo:cron';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Command description';

/**

* Create a new command instance.

*

* @return void

*/

public function __construct()

{

parent::__construct();

}

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

\Log::info("Cron is working fine!");

/*

Write your database logic we bellow:

Item::create(['name'=>'hello new']);

*/

}

}

Step 4: Register a task sheduler 

app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel

{

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

Commands\DemoCron::class,

];

/**

* Define the application's command schedule.

*

* @param \Illuminate\Console\Scheduling\Schedule $schedule

* @return void

*/

protected function schedule(Schedule $schedule)

{

$schedule->command('demo:cron')

->everyMinute();

}

/**

* Register the commands for the application.

*

* @return void

*/

protected function commands()

{

$this->load(__DIR__.'/Commands');

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

}

}

Step 5: Run sheduler command to locally

php artisan schedule:run


After run above command, you can check log file where we already print some text. so open you your log file it looks like as bellow:

storage/logs/laravel.php


At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:


For live cpanel:-


cd /your-project && php artisan schedule:run >> /dev/null 2>&1











No comments

Theme images by fpm. Powered by Blogger.