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

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

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

Scheduler command is not working at midnight in 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.

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 5.1 Task Scheduling on Windows

I'm trying to get Laravel 5.1 Task Scheduling working on IIS. When I run a batch file using Windows task manager it will run the task one time only. How can I get ->everyMinute() to work?
Windows batch file:
cd c:\inetpub\myapp
c:\PROGRA~2\PHP\php.exe artisan schedule:run 1>> NUL 2>&1
The kernel:
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\MyCommand::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('test')->everyMinute();
}
}
The command:
public function handle()
{
log::info('test');
}
Take a look at the task scheduler doc.
Starting The Scheduler
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.
In your case, you use the Windows task scheduler instead of Cron but the important thing is to call artisan schedule:run every minute. Each time this command is run, it checks its schedule and runs the due tasks/commands added.
artisan schedule:run does not start a long-running process that stays alive to runs tasks until you kill it. As I said, it needs to be called every minute.
You need to create a scheduled task that will execute that batch file every minute.
To do so :
Press Win + R and run taskschd.msc
In the right panel click Create Basic Task and give it a Name + Description.
Click Next and select Start a Program option, then navigate to the batch file and select it. No need to fill the other fields.
Select "Open the properties of this task..." and then Finish.
On the Trigger tab, you can change between Daily or At Logon (as I do).
Here is the part that's not documented, open the dropbox and insert 1 using the keyboard, this is the only way to set the repeat time at 1 minute (even if the dropdown doesn't list it).
Larevel needs the cronjob to run every minute, otherwise it won't work as expected.
Also check "Indefinitely" to run it forlifetime.
Hope it helps.
The Windows Task Scheduler Help is here, if you run into trouble.
I have a single solution
Create to file Executable xxx.cmd, Open the file and write the next text.
#echo off
echo - = = = Schedule Run Jobs == = = = -
CD d: && CD \xampp\htdocs\folderlaravel && php artisan schedule:run
timeout 86400
CD d: && CD \xampp\htdocs\folderlaravel && "Schedule.cmd"
pause
#cls
What you do is run and run itself in an infinite loop depending on what timeout you are given. In this case 86400 => 1 day.
It is somewhat ambiguous but it works :)
I hope it works for you.
Windows does support Laravel Scheduler but, you've to run the command on your own for multiple times. Since we can't use Windows Task Scheduler to run for every 1 min as we can do with linux crontab. If you're using windows for development environment and want to test if command is working on not you can try this
If you run the
php artisan schedule:run
command for multiple times by giving a min gap for each trial it'll work.
If you want to run directly the command you can follow this.
"path\to\php.exe" "artisan" YourCommand > "NUL" 2>&1 &
You can find path of your php.exe using below step.
Run "where php.exe" in command prompt

Resources