Scheduler command is not working at midnight in Laravel - laravel

I have setup a command to run every Saturday at midnight but it is not working
I write this but not running
$schedule->command('weeklyRewardPoints')->weeklyOn(6, '00:00')->timezone('Asia/Dubai');
then I try this but again not working
$schedule->command('weeklyRewardPoints')->weeklyOn(6, '0:0')->timezone('Asia/Dubai');
Even I tried to run at any specific minute at midnight like this but still not working
$schedule->command('weeklyRewardPoints')->weeklyOn(6, '00:30')->timezone('Asia/Dubai');
It is working successfully any other hour and minutes but not at midnight hour and minutes
this is my cron job
* * * * 6 php /home/path-to-my-project/artisan schedule:run >> /dev/null 2>&1

Taken from the Laravel docs
So, when using Laravel's scheduler, we only need to add a single cron configuration entry to our server that runs the schedule:run command every minute.
the scheduler need cronjob needs to run every minute
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
your scheduler runs every 6 minutes.
You can furthermore write
$schedule->command('weeklyRewardPoints')->weeklyOn(6)->timezone('Asia/Dubai');
and remove the time from weeklyOn as it is predefined with 0:0.

Related

Laravel 7: Cron jobs for task scheduler in Cpanel not working

I'm using Namecheap Hosting. And my files are in a subdomain. I've added a task scheduler in Kernel.php file and add cron jobs in hosting. But it's not working. When I manually run schedule command it works perfectly. Can anyone please see the below code and details and tell me what am I missing?
Kerner.php file:
protected function schedule(Schedule $schedule)
{
$schedule->command('add:earnLeave')
->everyMinute()
->timezone('Asia/Dhaka');
}
Cron Command:
/usr/local/bin/php /https://subdomain.maindomain.com/php artisan schedule:run >> /dev/null 2>&1
and in my hosting the minimum run time is every five minutes */5 * * * *
and my task scheduler will run once on the last day of the month. For testing purposes, I set it to every minute. Should I have to keep this run time equal to cron jobs?
What should I do now?
Domain name no needed.
/usr/local/bin/php /home/username/public_html/laravelrootfolder/artisan schedule:run >> /dev/null 2>&1
I have solved this problem myself.
I use /usr/bin/php instead of usr/local/bin/php
And also proc_open was disabled. After enabling it, now it's working fine.

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?
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.
59 * * * *
repeat at [Minutes] [Hour] [Day of Month] [Which Month] [Which Week]
use the above value to do it every 60 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 59 minutes, plus have mentioned already there about the star astreriks for you to test and check along with the url of resource

Setting up a Laravel cron job in cPanel

I have the following function:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:users')->everyMinute();
}
when I run the command
artisan schedule:run
it sends an email but when I add the following command to the cpanel as a cron job it doesn't send any email. Cpanel suppose to email me a notification when the cron job is run but I haven't receive a single email.
php /home/rain/artisan schedule:run 1>> /dev/null 2>&1
Where am I doing wrong?
Also when I run the command artisan schedule:run it runs it only once. I am very curious why do I have to add ->everyMinute(); if it is not going to run every minute? If I want to send it weekly I can setup the cron job. Why do I have to write to add ->weekly(); in the function if cron job is sending it weekly?
The Laravel scheduler assumes you have a cronjob every minutes. The scheduler is only useful if you want to have multiple tasks.
Normally you have one single cronjob configured in cPanel and you can set the scheduler to everyWeek() and have another task that would be everyDay() without having to add of change the cronjobs in your cPanel.
Laravel will automagically know if the task has already been run.
https://laravel.com/docs/5.4/scheduling
This Cron will call the Laravel command scheduler every minute. When
the schedule:run command is executed, Laravel will evaluate your
scheduled tasks and runs the tasks that are due.
It worked for me:
First try your command without waiting:
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run
Then, once you checked if it worked, add this:
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null 2>&1
This Works for me
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null
Just Make Sure you use exact version of php to execute schedule
e.g
if your php v is 7.3 then the code will be
/usr/local/bin/ea-php73 /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null
This worked for me
/usr/local/bin/php /home2/maildoll/demo.maildoll.com/artisan queue:work --stop-when-empty >> /dev/null

Laravel Task Scheduling set to run every minute but it run only once

I am using Laravel Task Scheduling. I defined a custom command and set it to run every minute like:
$schedule->command('dailyk')->everyMinute();
Then I used the following command to run the task:
php /var/www/stockhit/artisan schedule:run 1>> /dev/null 2>&1
I used log to check that my custom command continued to run. However, it is not run every minute. Instead, it just ran once.
How can I make it run every minute, instead of just one time?
See Task Scheduling:
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.
Laravel's task scheduler does not stay in memory, it needs to be run every minute. It will then check which tasks need to be run in that minute and run them.
When you run the task scheduler using PHP it just runs once, it needs cron to run it every minute.
you need to add a cron job. On ubuntu use the command
crontab -e
to open your cron job file, then add
* * * * * php /var/www/stockhit/artisan schedule:run 1>> /dev/null 2>&1

Godaddy Cron Job Running every seconds in cpanel

I need to load a file every seconds, which means, there is not a determinate time of the day.
I am using cPanel to run the CronJob and the task (the wget) is already working fine and I am just having trouble with the time schedules.
I have tried:
Minute Hour Day Month Weekday
*/1 * * * *
running every minute:
Minute Hour Day Month Weekday
* * * * *
it seems a bit odd why you run it every second. Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 1 seconds. The alternative is to write a shell script that uses ‘sleep 1′ command in it.
$ cat every-1-second.sh
#!/bin/bash
while true
do
/home/ramesh/backup.sh
sleep 1
done

Resources