There are many occasions where we need to run a job at a certain time or after/before a certain event as part of our project. We achieve this by implementing various forms of scheduling. That said, scheduling is not one of the straightforward things to do. So, more often than not, it gets confusing.
Scheduling in Laravel however, is handled in a very elegant and obvious way. This leaves less room for error for developers like me who are fine with handling complex things but screw up at simpler and more obvious things!
Since I wrote a post about How To Create a Custom Laravel Command, just a few days back, in this quick tutorial about how to schedule jobs in Laravel, I will build upon the same post and show how we can schedule the command that we had created in that post. If you would like to get a full context of the thing, I suggest you go through that post first, here.
Assuming you have gone through the above-mentioned post about how to create a new Laravel command, let’s now proceed to see how can we schedule this command. It’s much simpler than it sounds. Let’s see how.
How To Schedule A Laravel Command?
In order to schedule a command (or even directly call a Laravel Controller function – more on that later), all we need to do is open the app\Console\Kernel.php.
Once there, we will schedule our log:write command that we created here, to run every minute. To do that, all we need to is, update the schedule method like this:
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('log:write')->everyMinute();
}
Then, we can run the command by
We can run the said command at various other intervals. The complete list of possibilities can be found on Laravel’s official documentation page, here.
Bonus Tip
Just like scheduling Laravel commands, you can also schedule calls to Laravel Controller methods. The same can be done by changing/adding the body of the schedule function as follows:
$schedule->call('App\Http\Controllers\YourControllerName@YourFunctionName')->everyMinute();
Run Laravel Scheduled Commands Automatically
In order to make sure Laravel keeps running the scheduled command, at your set interval, you have to run the Laravel Schedule Worker. You can do it by typing the following command in your terminal, inside your project’s root directory.
php artisan schedule:work
Running the Laravel Scheduler Automatically, In Production
In a production environment, in order to make sure the scheduler is run at regular intervals, as set, you will have to add an entry in the server’s crontab. In order to edit the crontab file on the production server, run the following command on the terminal, once you are inside your project’s root directory.
crontab -e
This is how the entry would look:
* * * * * cd /your-project-root-directory && php artisan schedule:run >> /dev/null 2>&1
That’s all about scheduling a command.
This project can be downloaded from GitHub, here.
3 thoughts on “How To Schedule Jobs or Commands in Laravel?”
Pingback: How To Renew SSL Using bncert-tool on Amazon Lightsail - Bitnami LAMP Stack? - Rajiv Verma
Pingback: How To Change Browser Scrollbar colors using CSS? - Rajiv Verma
Pingback: How To Create a Custom Laravel Command? - Rajiv Verma