How to cronjob task schedule every 40minutes? - laravel

How to run 1 job every 40 minutes or 45 minutes in laravel. I have read the laravel documentation with the cron('') function that only performs the correct action for the time specified in cron(''), it does not have to be repetitive after a while.

You can do it by creating 3 crons in the Scheduler:
...->cron('00,45 */3 * * *');
...->cron('30 1-22/3 * * *');
...->cron('15 2-23/3 * * *');
Adapted from: https://www.linuxquestions.org/questions/linux-enterprise-47/cron-every-45-minutes-4175501255/page2.html for laravel

Related

InvalidArgumentException : Invalid CRON field value 30 at position 4

I tried To run this command php artisan schedule:run on laravel project I got this error
InvalidArgumentException : Invalid CRON field value 30 at position 4
at /var/www/vendor/dragonmantank/cron-expression/src/Cron/CronExpression.php:155
151| */
152| public function setPart($position, $value)
153| {
154| if (!$this->fieldFactory->getField($position)->validate($value)) {
> 155| throw new InvalidArgumentException(
156| 'Invalid CRON field value ' . $value . ' at position ' . $position
157| );
158| }
159|
this is command on my Kernel
protected function schedule(Schedule $schedule)
{
// Schedule to delete old messages every old days
$schedule -> command(DeleteOldMessages::class, ['days' => config('marketplace.days_old_messages')])
->days(config('marketplace.days_old_messages'));
// Make the command for releasing purchases runs each X days
$schedule -> command(ReleasePurchasesCommand::class, ['days' => config('marketplace.days_old_purchases')])
->days(config('marketplace.days_old_purchases'));
// Run completing command for purchases every defined number of days
$schedule -> command(CompletePurchaseCommand::class) -> days(config('marketplace.days_complete'));
}
I dont know what issue is
Thanks in advance for any help.
The scheduler ->days() function is expecting an array of days of the week on which the task should run. For instance ([1,3,5]) meaning run on Monday,Wednesday and Friday)
I assume with values of 20 and 30, you are expecting the tasks to run at a specified interval. This is not possible with cron based scheduling.
What you probably want is to run the tasks every day and within the task, determine if any records exceed the specified limits.

SpringBoot Scheduler Cron over running

is there any expert having the issue using springboot scheduler
trying to set it to run between 2pm til 10pm on weekday every 15mins/per hour, but it seem like trigger by minute, is that because my cron is wrong or i shld do smthg to control it ?
running in linux server via springboot-web-started
#Scheduled(cron = "0 15 14-22 * * MON-FRI")
private void fireDownload() {
log.info("fireDownload");
this.jmsXXXX.run(Constants.XXXX);
}
version
spring-boot 2.4.2
java 11
Please try this
#Scheduled(cron = "0 */15 14-22 * * MON-FRI")
You say in a comment that this is not working, so let's test this with a simple proof-of-concept that fires every 5 minute
#Scheduled(cron = "0 */5 8-22 * * MON-FRI")
private void cronPOC() {
log.info("cronPOC triggered by cron");
}
Screen-shot below shows that the POC is indeed working.
While we're at testing, let's put #GerbenJongerius suggestion from comment above to the test as well (with some tiny changes in order to speed things up).
#Scheduled(cron = "0 0/5 8-22 ? * MON-FRI")
private void cronPOC() {
log.info("cronPOC triggered by cron v2");
}
... and this is also working
Some Spring cron examples with explanations here:
https://stackoverflow.com/a/26147143/14072498

scheduled cron expression that never runs

What I tried:
#Scheduled(cron="* * * 08 04 2099")
I want cron expression that never executes.can any one help me with the expression.
Thanks in advance...!
This cron will run every minute and task will be bound with condition.
If you need different cron job then you can generate using this website.
#Scheduled(cron = "0 0/1 * 1/1 * ? *")
protected void performTask() {
if (condition)//if value matches with database value
{
//perform the task
}
}
You can use.
#Scheduled(cron = "${cron.expression}")
cron.expression=-
This works from Spring 5.1.
See the docs.

Laravel : Task Scheduling [ In Parallel ]

I have multiple tasks that need to be done every hour or two. All of them have been scheduled via Laravel using below comamnds as cron jobs
$schedule->command('email:notifications1')
->cron('15 * * * * *');
$schedule->command('email:notifications2')
->cron('15 * * * * *');
$schedule->command('email:notifications3')
->cron('15 * * * * *');
Issue:
All of the above tasks are pretty time consuming & it seems from the results that these tasks are not running in parallel. And each tasks runs after the previous has ended.
Requirment
How can i run them in parallel? I want all tasks to be executed (in parallel) as soon as the clock tick the specified time.
Laravel Version 5
You can easily have multiple parallel commands running if you add runInBackground() to the chain.
Like this:
$schedule->command('email:notifications1')
->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications2')
->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications3')
->cron('15 * * * * *')->runInBackground();
This creates a new process in the background so the Scheduler doesn't have to wait until the command executes. This doesn't even interfere with the withoutOverlapping() method because that works with mutex files.
Now you also have the benefit of having your commands in version control.
The Laravel scheduler can only run one command at a time because of the limitations of PHP.
You could, however, add the cronjobs directly in your crontab file, this way, they will be executed in parallel in separate processes.
15 * * * * * php /path/to/artisan email:notifications1
15 * * * * * php /path/to/artisan email:notifications2
15 * * * * * php /path/to/artisan email:notifications3
Another way to fix this is to let the jobs start at another time. Because a new php process is started every minute by the cron job, these do not affect each other.
For example:
$schedule->command('email:notifications1')
->cron('5 * * * * *');
$schedule->command('email:notifications2')
->cron('10 * * * * *');
$schedule->command('email:notifications3')
->cron('15 * * * * *');

Multiple cronjob emails

I have 3 jobs in my crontab. I want to recieve emails if only 1 of them fails and not for other two. Is there any way to restric emails to one type of cronjob?
Redirect the output of the two you don't care about to /dev/null if you don't ever want to see the output or to some file if you do.
Your cron likely supports this:
# This job produces mail.
* * * * * echo Hello
# These jobs do not.
MAILTO=
* * * * * echo Foo
* * * * * echo Bar

Resources