In Laravel applications, automating tasks like sending emails, generating reports, or clearing caches at specific intervals is crucial. This is where Laravel cron job scheduling comes into game, allowing artisans to execute predefined commands or scripts automatically. This article will guide you through setting up and managing cron jobs in Laravel, leveraging its powerful scheduling feature
Laravel Setup for Cron Job Scheduling
To begin scheduling tasks in Laravel, ensure you have a working Laravel application set up. If not, install Laravel using Composer.
Create New Command
In this step, we need to create our custom artisan command. That command will execute with task scheduling cron job. so, let’s run bellow command to create new custom command.
Now let’s modify this command file.
app/Console/Commands/DemoCron.php
Register Scheduler
In this step, we need to define our commands in routes/console.php file with time when we want to run our command like as bellow functions that used to schedule our command or task
So let’s schedule our task
routes/console.php
now we are ready to run our cron command, so you can manually check using following command of your cron. so let’s run bellow artisan command:
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
Refrences
OR
Source Code
https://github.com/techsoucedev/cron-job-demo