Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I schedule a laravel task on a specific day of the month?

Now reading through the docs I don't see a direct function for that, but I do see the option to use the monthly() method combined with a when() method, so I thought, could I do this maybe:

$schedule->command('send:reminders')->monthly()->when(function() {
    return date('d') == '23';
});

But now I'm afraid that won't work, because as far as I can see it will try the when() constraint only once a month (probably not on the date that I want it to) and then when it fails it skips that month. At least that's what I'm guessing from reading the source of laravel.

So then I'm lost, how do I make this happen?

like image 340
Evert Avatar asked Jan 20 '16 10:01

Evert


People also ask

How do I run a specific schedule in laravel?

For example, you may schedule a command to run weekly on Monday: // Run once per week on Monday at 1 PM... })->weekly()->mondays()->at('13:00'); // Run hourly from 8 AM to 5 PM on weekdays...

How do I schedule a cron job in laravel?

Scheduling artisan commands Laravel has multiple methods of implementing cron jobs, and so far we've seen how to use closures to schedule these tasks. The other method is the use of artisan commands to manage tasks in an application. Laravel provides an interactive command-line tool to create commands at our disposal.


1 Answers

The easiest way might be to make use of the cron method so in your case

$schedule->command('send:reminders')->cron('0 0 23 * *');

That is saying run at midnight on the 23rd day of the month.

like image 69
Mark Davidson Avatar answered Oct 31 '22 16:10

Mark Davidson