I am trying to send an email hourly using the Task scheduler in (laravel) framwork, the command I am typing in the kernal.php: $schedule->command('cron:activeUsers')->hourly()->withoutOverlapping();
than put the following in crontab -e:
-
-
-
-
- cd /testEmail && php artisan schedule:run >> /dev/null 2>&1
-
-
-
but I am getting like 60 or even 100 email instead of one, I put some logs to see what is happening: the cron job is excuting and sending email on the first 60 second of the first minute of each hour like the following for example: 12:00:00 12:00:01 12:00:02 12:00:03 12:00:04 12:00:05 . . 12:00:59
how I can prevent the cron job to be excuted like this ? I want the cron job to be excuted only once each hour. Am I using the wrong method to send the email?
CodePudding user response:
The schedule:run
command is supposed to be called every minute (as fast as CRON allows), like so:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
See https://laravel.com/docs/9.x/scheduling#running-the-scheduler
Then, your schedule()
function in app/Console/Kernel.php
defines the interval for each of your tasks, so your ->hourly()
command should execute at the start of every hour.
If it is executing any faster than that, its likely that you somehow happen to call the scheduler more often than once a minute (multiple CRONs maybe for the same command?). Try a debug statement in the schedule()
function in app/Console/Kernel.php
to see that it actually executes once a minute.
When you put a debug statement inside your actual command (i.e. 'cron:activeUsers'
), then you should see that it runs every hour.
CodePudding user response:
59 * * * *
repeat at [Minutes] [Hour] [Day of Month] [Which Month] [Which Week]
use the above value to do it every 59 minutes, If want to check it, you can also use https://crontab.guru/ to verify what exactly you want.