1

I have made a custom command and want it to run on month last day at 02:00 but I also want it to run after that between some period of time like 02:00 till 15:00 following is my scheduler

`$schedule->command('billing:generate')
                ->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00");`

now what I think Ill do to achieve this is like this:

$schedule->command('billing:generate')
            ->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00")
            ->between("02:00", "15:00");

will it work as I want? I am on laravel version 6.

Taha Malik
  • 21
  • 6

1 Answers1

0

As you are on Laravel 6, you can schedule your command to run every day, but just check is that day the last day of the month, something like this:

// Runs exactly on "02:00" every month on its last day.
// (Actually runs every day, but doesn't execute if the day is not the last day of the month).
$schedule->command('billing:generate')->dailyAt('02:00')->when(function () {
    return Carbon::now()->endOfMonth()->isToday();
});

// Runs between "02:00" and "15:00" every month on its last day. 
// (Actually runs every day, but doesn't execute if the day is not the last day of the month).
$schedule->command('billing:generate')->daily()->between("02:00", "15:00")->when(function () {
    return Carbon::now()->endOfMonth()->isToday();
});

If you are on newer versions of Laravel you can use lastDayOfMonth to which you can provide a time, and if needed, you can also chain between on it.

Also, to achieve what you want, you can specify 2 schedules and resolve the problem.

It should be like this:

// Runs exactly on "02:00" every month on its last day. 
$schedule->command('billing:generate')->lastDayOfMonth("02:00");
// Runs between "02:00" and "15:00" every month on its last day. 
$schedule->command('billing:generate')->lastDayOfMonth()->between("02:00", "15:00");
Ben
  • 2,060
  • 9
  • 21
  • Sorry I forgot to mention I am on laravel version 6 which does not support lastDayOfMonth() – Taha Malik Mar 22 '22 at 11:58
  • I updated the answer to make it Laravel 6 way. – Ben Mar 22 '22 at 12:06
  • Ok I will but I have one more thing to ask if I have to use ->withoutOverlapping() where should I put it in? – Taha Malik Mar 22 '22 at 12:23
  • You can chain it at the end. It shouldn't start the same new command before the first one is completed. So just add it to the end. – Ben Mar 22 '22 at 12:27