How can I run this cron? - laravel

I have some schedules that serve to clean the DB at certain times, but I want to use cron to run these times but when I try to run them in the windows schedule I get the following error:
6 is not a valid position
at C:\laragon\www\Sis\vendor\dragonmantank\cron-expression\src\Cron\FieldFactory.php:46
42| case 4:
43| $this->fields[$position] = new DayOfWeekField();
44| break;
45| default:
> 46| throw new InvalidArgumentException(
47| ($position + 1) . ' is not a valid position'
48| );
49| }
50| }
1 C:\laragon\www\Sis\vendor\dragonmantank\cron-expression\src\Cron\CronExpression.php:154
Cron\FieldFactory::getField()
2 C:\laragon\www\Sis\vendor\dragonmantank\cron-expression\src\Cron\CronExpression.php:137
Cron\CronExpression::setPart("*")
And I do not have much experience with crons, as far as I know, it should take 6 asterisks and you specify in minutes, hours, days, months, how can I fix them?
This is the code:
protected function schedule(Schedule $schedule)
{
$schedule->command('S:LA')->cron('0 23 4 1 * *');
$schedule->command('S:LC')->cron('0 23 4 2 * *');
$schedule->command('S:LC')->cron('0 23 4 6 * *');
$schedule->command('S:LC')->cron('0 23 4 10 * *');
$schedule->command('S:LD')->dailyAt('00:00');
$schedule->command('S:LM')->monthlyOn(4, '23:00');
$schedule->command('S:LS')->mondays()->at('23:00');
$schedule->command('S:LSM')->cron('0 23 4 6 * *');
$schedule->command('S:LSM')->cron('0 23 4 1 * *');
$schedule->command('S:LT')->cron('0 23 4 1 * *');
$schedule->command('S:LT')->cron('0 23 4 3 * *');
$schedule->command('S:LT')->cron('0 23 4 6 * *');
$schedule->command('S:LT')->cron('0 23 4 9 * *');
$schedule->command('S:LT')->cron('0 23 4 12 * *');
}

->cron('* * * * *'); Run the task on a custom Cron schedule
->everyMinute(); Run the task every minute
->everyTwoMinutes(); Run the task every two minutes
->everyThreeMinutes(); Run the task every three minutes
->everyFourMinutes(); Run the task every four minutes
->everyFiveMinutes(); Run the task every five minutes
->everyTenMinutes(); Run the task every ten minutes
->everyFifteenMinutes(); Run the task every fifteen minutes
->everyThirtyMinutes(); Run the task every thirty minutes
->hourly(); Run the task every hour
->hourlyAt(17); Run the task every hour at 17 minutes past the hour
->everyTwoHours(); Run the task every two hours
->everyThreeHours(); Run the task every three hours
->everyFourHours(); Run the task every four hours
->everySixHours(); Run the task every six hours
->daily(); Run the task every day at midnight
->dailyAt('13:00'); Run the task every day at 13:00
->twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
->weekly(); Run the task every sunday at 00:00
->weeklyOn(1, '8:00'); Run the task every week on Monday at 8:00
->monthly(); Run the task on the first day of every month at 00:00
->monthlyOn(4, '15:00'); Run the task every month on the 4th at 15:00
->quarterly(); Run the task on the first day of every quarter at 00:00
->yearly(); Run the task on the first day of every year at 00:00
->timezone('America/New_York'); Set the timezone

Related

Spring Scheduler to run at different timings on a single day

I have a requirement where my scheduler has to run for every hour between 9 AM to 12 PM everday and then it runs once by 3PM and 5 PM respectively.
How can i make my CRON expression to work according to the requirement
So far i have tried with below cron expression
#Scheduled(cron="0 0 9-12 * * *")
But that runs for every hour from 9 AM - 12 PM
How can i modify it to make it run once by 3 PM and 5 PM also.
I have tried by combining two cron expressions like below but that dint work.
#Scheduled(cron="0 0 9-12 * * *, 0 0 3 * * *")
Can i give 2 cron expressions through 2 #Scheduled like below
#Scheduled(cron="0 0 9-12 * * *")
#Scheduled(cron="0 0 9-12 * * *")
public void myMethod() {}
Would that be a feasible solution?
Can anybody help me with this?
#Schedules annotation can be used to set multiple calendar-based timer expressions.
This annotation will get rid of the redundancy of writing the same
method multiple times.
In the following example, the first expression sets a timer to expire on the last day of every month. The second expression sets a timer to expire every Friday at 11:00 PM.
#Schedules ({
#Schedule(dayOfMonth="Last"),
#Schedule(dayOfWeek="Fri", hour="23")
})
public void doPeriodicCleanup() { ... }

Problem with cron job which has to run every 15 minutes but not at 15,30,45

So i have a cron job that runs and executes fine (hence why no path is shown) at:
*/15 * * * *
but I don't want that as it means it runs at every fifteen minutes past 0 of the hour according to crontab.guru which gives tasks run at 15,30 and 45 minutes.
This is actually what I want:
1/15 * * * *
which according to crontab.guru should begin the 15 minutes at 1 minute past the hour and run the tasks at 16, 31 and 46 minutes.
Now the problem is when I crontab -e to set up the cron job of 1/15 * * * * I get a bad minute error from crontab, yet as i say in my opening lines using */15 * * * * returns no error from crontab and the task is executed. Why is this?.
You could do 1,16,31,46 * * * * to run the jobs at 1, 16, 31 and 46 minutes every hour.

spring Scheduler -runs once in a week

I have to write a spring scheduler which runs once in a week , say every Monday at 1 AM .What will be the cron expression for that , can we archive this using fixedDely.
You can use cron for that:
#Scheduled(cron = "0 0 1 * * MON")
In order from left to right of a cron expression:
second, minute, hour, day of month, month, day(s) of week
A fixed delay is a delay, not a recurring event. With fixedDelay you could setup an event that runs after a fixed period in milliseconds between the end of the last invocation and the start of the next. That's not what you want here.
To have a job run every monday at 1 AM you can setup a cron expression
#Scheduled(cron = "0 0 1 * * MON")

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.
$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.
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'
$schedule->command('log:test')->cron('0 0 */14 * *');
2017-09-15 00:00:00
2017-09-29 00:00:00
2017-10-01 00:00:00
2017-10-15 00:00:00
2017-10-29 00:00:00
or you can run your custom
$schedule->command('log:test')->weekly()->when(function () {
return ((Carbon::now()->day % 2) === 1); //bool only for example
});

cron job for every half hour

i have to run a shell script as cron job
in VERY HALF HOUR but ONLY IN BETWEEN (morning 10.30 A.M to evening 6.30 A.M)
DAILY
means this cron job will be execute every half only within a scpecfic givine time
This will do:
30 10 * * * <your cronjob>
0,30 11-18 * * * <your cronjob>
Note you can also use the following for every 30 min:
*/30 11-18 * * * <your cronjob>

Resources