Example of Cron Job in Laravel 5

By Hardik Savani | February 6, 2016 | | 11128 Viewer | Category : Laravel


Share this post:

Whenever you want to define your scheduler task, and if you are working on laravel framework then you can do it easy way and very secure. Laravel provide Scheduling and Scheduling through you can manage your cron, if you need to run every day, every minute, every hourly, weekly, on specified days.So, basically i going to give example how to create cronjobs in laravel.

First you have to create one command that way you can create proper programming and good way, but you can also do without create command, It is good if you create your own command and with good code. So, open your terminal or cmd and fire command as i give you under:

Create Command:

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

After this command you can see "app/Console/Commands/DemoCron.php" in your laravel folder.

I give you example to when you fire your this command then it will insert one record in your "items" table. so if in your database have no that table then create(id,name) for testing and this file is command file, now you have to change that file this way:

app/Console/Commands/DemoCron.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use DB;

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

{

DB::table('items')->insert(['name'=>'hello new']);

$this->info('Demo:Cron Cummand Run successfully!');

}

}

Now we have to define our new command in Kernel.php(app/Console/Kernel.php) file.first open that file and change it this way:

app/Console/Kernel.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\Inspire::class,

Commands\DemoCron::class,

];

/**

* Define the application's command schedule.

*

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

* @return void

*/

protected function schedule(Schedule $schedule)

{

$schedule->command('inspire')

->hourly();

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

->everyMinute();

}

}

Now you are ready for run command:

php artisan demo:cron

after this command you can see in your terminal or command prompt:

Demo:Cron Cummand Run successfully!

Now i added cron scheduling on every minute, you can check in your "app/Console/Kernel.php" file schedule() fucntion like:

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

->everyMinute();

But you can change everyMinute into every day using daily() function, you can also set yearly, monthly, weekly for more information : Scheduling Task

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

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1