Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run laravel cron after every 2 weeks or 14 days

I need to run laravel cron for after every 2 weeks or 14 days. but not found any solution for this. I was also search on laravel documentation https://laravel.com/docs/5.5/scheduling and found this option

->weekly();

But it runs on weekly. I was search also other option and found this laravel schedule task to run every 10 days of a month But it work only first 10 days of a month

$schedule->command('log:test')->cron('0 0 1-10 * *');

Please help me if you have any solution thanks in advance.

like image 279
ankit Avatar asked Sep 11 '17 07:09

ankit


People also ask

How do I set a cron schedule?

Cron jobs are scheduled at recurring intervals, specified using a format based on unix-cron. You can define a schedule so that your job runs multiple times a day, or runs on specific days and months. (Although we no longer recommend its use, the legacy App Engine cron syntax is still supported for existing jobs.)


2 Answers

You can use daily() together with when() method and inside when add suitable constrain for your task. For example, run task every month on 1,16 days:

$schedule->command('sitemap:sitemap_xml_generate')->daily()->when(function () {
$days = [1,16];
$today = Carbon::today();
return in_array($today->day, $days);});

or you can use this

$schedule->command('sitemap:sitemap_xml_generate')->cron('0 0 1,16 * *');

cron('0 0 1,16 * *') -> Argument are 'minute hour day(we can specify multiple date seperated by comma) month year'

like image 137
Harsha Avatar answered Oct 14 '22 17:10

Harsha


$schedule->command('command_name')->weekly()->mondays()
         ->when(function () {
             return date('W') % 2;
          })->at("13:38");

Function will return 1 in every two other weeks, So the command will be called in every two weeks.

like image 20
Alican Ali Avatar answered Oct 14 '22 16:10

Alican Ali