Task Scheduling of Laravel

Open Task Scheduling file

Open yourProjectName/app/Console/Kernel.php

Config your schedule

Here is a schedule example

protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
Token::where('expiry_time', '<', time())->delete();
PaymentServiceOrders::deleteExpiredOrders();
Order::where('expiry_time', '<', Carbon::now())->delete();
})->daily();
}

In my case, it’s to daily delete the expired orders

Add the Task Scheduling into crontab

  1. sudo vim /etc/crontab
  2. * * * * * apache cd /var/www/html/yourProjectName && php artisan schedule:run >> /dev/null 2>&1
  • Here are the meaning of * in orders
    1. Minute(0-59)
    2. Hour(0-23)
    3. What date in a month(1-31)
    4. Month(1-12)
    5. What day in a week(0-6)
  • apache
    it represents the user. It’s important here, because if you don’t set it properly, when error occurs as you execute the schedule, the log owner will be the user you set here, which might have authority problem. If you also log other information in other place, and the whole project might not be able to work properly when the log file reject to be written.
  • cd ray cd /var/www/html/yourProjectName
    To where your project is
  • php artisan schedule:run >> /dev/null 2>&1
    Run the Task Scheduling command in your Laravel project
  1. Here you go!
Handle AllPay third party payment service with Laravel Send email via AWS SES in Laravel

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×