Example of Cron Job in Laravel

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog i will talk about Cron job in laravel. When we want to define our scheduler task, and also if we are working on laravel framework then we can do this very easy way and very secure. we know that laravel provide Scheduling and Scheduling through we can manage our cron job, if we need to run it every day, every minute, every hourly, weekly, or on specified days.So, it basically i am going to give you a example how to create cron jobs in laravel application.

At First we need to create one command that way we can create proper programming and a good way, but we can also do it without create any command, It is good if we create our own command and also with good code. So, need to open our terminal or cmd and then need to fire command as i give you under:

Create Command:

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

So after this command we can see “app/Console/Commands/DemoCron.php” in our laravel folder.

Now here I will give you an example to when we fire our this command then it will insert one record in our “items” table. So if in our database have no that table then create(id,name) for testing and on this file is command file, now we need 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!');
    }
}

So now we have to define our new command in Kernel.php(app/Console/Kernel.php) file. At first need to 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 we are ready for run command:

php artisan demo:cron

So after this command we can see in our terminal or command prompt:

Demo:Cron Cummand Run successfully!

Now we can added cron scheduling on every minute, we can check in on our “app/Console/Kernel.php” file schedule() function like:

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

But we can change it every Minute into every day by using daily() function, we can also set yearly, monthly, weekly for more information : Scheduling Task

So at last we can manage this command on scheduling task, we need to add a single entry to our server’s crontab file:

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

Read Also : How to push item to 0 index or first of $scope object in AngularJS?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →