Cron Job In Laravel 8- Setting up Cron in windows pc
Hello everyone in today’s article we will get to know about how we can set create Cron Job In Laravel 8 as well as how to set up Cron in windows pc, before moving forward let’s know what is Cron
What is Cron?
The software utility Cron is also known as Cron job is a time-based job scheduler in Unix-like or windows computer operating systems. Users that set up and maintain software environments use Cron to schedule jobs to run periodically at fixed times, dates, or intervals.
for more information related to the cron please visit the URL
To create the Cron Job in Laravel 8 we will follow the below steps.
- Install the application
- create the command
- Register Task Scheduler
- Run the task scheduler
- Create the Cron job in windows
Step-1: Download the Laravel Application
Open your command prompt under your htdocs and run the below command
composer create-project laravel/laravel laravel-project
Step-2: Create the Command
In this step, we need to create our custom command. the custom command will execute with the task scheduling cron job. so, let’s run the below command to create a new custom command.
php artisan make:command TesCron --command=test:cron
When you run the above command a file under app/console/command is being created named TestCron.php
open that file and make some changes as like below
<?php
  Â
namespace App\Console\Commands;
  Â
use Illuminate\Console\Command;
  Â
class TestCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test: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()
    {  Â
        //Here you can write your business logic
        \Log::info("Huraay! Cron is working");
     Â
        $this->info('Test:Cron Cummand Run successfully!');
    }
}
Step -3: Register Task Scheduler
To make your command work, we need to register our command in Kernel.php, below is the list of task scheduler.
Method | Description |
---|---|
->cron('* * * * *'); | Run the task on a custom cron schedule |
->everyMinute(); | Run the task every minute |
->everyTwoMinutes(); | Run the task every two minutes |
->everyThreeMinutes(); | Run the task every three minutes |
->everyFourMinutes(); | Run the task every four minutes |
->everyFiveMinutes(); | Run the task every five minutes |
->everyTenMinutes(); | Run the task every ten minutes |
->everyFifteenMinutes(); | Run the task every fifteen minutes |
->everyThirtyMinutes(); | Run the task every thirty minutes |
->hourly(); | Run the task every hour |
->hourlyAt(17); | Run the task every hour at 17 minutes past the hour |
->everyTwoHours(); | Run the task every two hours |
->everyThreeHours(); | Run the task every three hours |
->everyFourHours(); | Run the task every four hours |
->everySixHours(); | Run the task every six hours |
->daily(); | Run the task every day at midnight |
->dailyAt('13:00'); | Run the task every day at 13:00 |
->twiceDaily(1, 13); | Run the task daily at 1:00 & 13:00 |
->weekly(); | Run the task every Sunday at 00:00 |
->weeklyOn(1, '8:00'); | Run the task every week on Monday at 8:00 |
->monthly(); | Run the task on the first day of every month at 00:00 |
->monthlyOn(4, '15:00'); | Run the task every month on the 4th at 15:00 |
->twiceMonthly(1, 16, '13:00'); | Run the task monthly on the 1st and 16th at 13:00 |
->lastDayOfMonth('15:00'); | Run the task on the last day of the month at 15:00 |
->quarterly(); | Run the task on the first day of every quarter at 00:00 |
->yearly(); | Run the task on the first day of every year at 00:00 |
->yearlyOn(6, 1, '17:00'); | Run the task every year on June 1st at 17:00 |
->timezone('America/New_York'); | Set the timezone for the task |
for more information about task scheduling please refer this link
Now go to app/Console/Kernel.php and write the below code
<?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\TestCron::class,
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('test:cron')
                 ->everyMinute();
    }
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
    Â
        require base_path('routes/console.php');
    }
}
Step-4: Run the scheduler command for test
open your command prompt and enter the below code
php artisan schedule:run
Now go to your storage/logs/laravel.log you can see the below output
[2020-12-24 03:46:42] local.INFO: Huraay! Cron is working Â
[2020-12-24 03:46:52] local.INFO: Huraay! Cron is working Â
[2020-12-24 03:46:55] local.INFO: Huraay! Cron is working Â
At last, you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:
Don’t go for the below code if you are doing in windows pc
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1 OR * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Step-5: Setting up Cron in windows
Go to your control panel and search for schedule task
when you open the task scheduler you have an image something like below
Click on task scheduler Library you have an image something like below
Click on create task under the name write artisan or whatever you want
Now click on the trigger tab then click on new, then popup like below image will we open just do the settings as per the image
Now go action tab and then click on new then the image like below will be open
Under the program/script you have to add your PHP location, for example, I will add the below path
F:\xampp\php\php.exe
In add argument list I will add my artisan path, for example I will use the below path.
F:\xampp\htdocs\laravel-project\artisan schedule:run
Now click ok, and save all the settings now on the left-bottom you can see the selected item click on run, that’s it now you Cron will run in every one minute.
I hope you enjoy the blog post, still having confusion feel free to comment.