'Hourly cron Job Excuting 60 times
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?
Solution 1:[1]
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.
Solution 2:[2]
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.
at 59, it tells to redo it every 60 minutes, plus have mentioned already there about the star astreriks for you to test and check along with the url of resource
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Flame |
Solution 2 |