How to set up scheduled tasks (scheduler) in Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
This article summarizes how to set up scheduled tasks (scheduler) in Laravel.
Environment
Laravel Framework 8.60.0
Getting Started
Regarding Laravel schedule settings,
It is written in this documentation, but this article covers the minimum necessary settings.
Assumes that the commands are created in advance
For information on creating commands, please refer to this article
Setting up the scheduler
Editing App\Console\Kernel.php
Scheduled tasks can be set up by writing in the schedule function.
The commands are using what was created in this article.
protected function schedule(Schedule $schedule)
{
// Run the command every hour
$schedule->command('asitakara-ganbaru')->hourly();
// Multiple commands can also be written
$schedule->command('asitakara-ganbaru')->daily();
}
Debugging scheduled tasks locally
You can run
php artisan schedule:work
to debug.
This command allows you to run the scheduled tasks in the local environment before setting them up on the server. It's convenient, isn't it?
When deploying to the server, please refer to the following.
Setting up on the server
Rough explanation of this scheduler functionality
php artisan schedule:run
executes the command every minute and compares the time set with hourly()
(every hour) previously written to determine whether to execute or not.
Therefore, you need to configure the server to run php artisan schedule:run
every minute
A common method for requesting the server to perform such functions is called cron.
The installation method may vary depending on the server or operating system.
For example, on AWS EC2, cron is already installed.
Example on EC2
On EC2,
/etc/crontab
is where you can configure the settings. So, edit with vim.
sudo vim /etc/crontab
When you open it, it will look like this
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Add the following (for initial settings on EC2, write to run as root user)
* * * * * root cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Replace path-to-your-project
with the directory of the Laravel project cloned from GitHub, for example.
Runs every minute
"Move to the directory of the Laravel project"⇨ "Run the schedule"
This is the command to have the server execute.
Final setting
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Debugging locally
Since setting up cron locally is cumbersome, Laravel provides the following command.
php artisan schedule:work
Use this command to check if it executes at the correct time.