Setting up a Laravel cron job in cPanel - laravel

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

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.

How to run queue worker on shared hosting

My Laravel application has a queued event listener and I have also set up the cronjob to run schedule:run every minute.
But I don't know how I can run the php artisan queue:worker command persistently in the background. I found this thread where it was the most voted approach:
$schedule->command('queue:work --daemon')->everyMinute()->withoutOverlapping();
However, on a different thread some people complained that the above-mentioned command creates multiple queue worker.
How can I safely run a queue worker?
Since Laravel 5.7, there's a new queue command to stop working when empty:
php artisan queue:work --stop-when-empty
As this is mostly just for emails or few small jobs, I put it on a cronjob to run every minute. This isn't really a solution for more than 100 jobs per minute I'd say, but works for my emails. This will run about 5 seconds every minute just to send emails, depending on how many emails or how big the job.
Steps
Create new command: php artisan make:command SendContactEmails
In SendContactEmails.php, change: protected $signature = 'emails:work';
In the handle() method, add:
return $this->call('queue:work', [
'--queue' => 'emails', // remove this if queue is default
'--stop-when-empty' => null,
]);
Schedule your command every minute:
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:work')->everyMinute();
// you can add ->withoutOverlapping(); if you think it won't finish in 1 minute
}
Update your cronjobs:
* * * * * /usr/local/bin/php /home/username/project/artisan schedule:run > /dev/null 2>&1
Source
Processing All Queued Jobs & Then Exiting
The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:
php artisan queue:work --stop-when-empty
are you using cpanel?
you can set in the Scheduler or Cron Jobs menu.
and set the command in there
You can set a schedule task like this
$schedule->command('queue:work --stop-when-empty')->everyMinute()->withoutOverlapping();

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

Laravel Schedule command does not work

I used laravel 5 in my project. I wanted to create a scheduler for inserting user. In kernel.php, I put my codes and set the scheduler.
I created a command class named "InsertUser" and put it in kernel.php $commands variable.
In command line, I ran "php artisan schedule:run". But I found "No scheduled commands are ready to run.". If I used call function instead of command function (in lernel.php), it was working fine. Please help me.
The Laravel Scheduler needs a cron job that runs the php artisan schedule:run command periodically, which in turn evaluates any scheduled commands and runs them accordingly.
From your screenshot I see you're running Windows, which means you can't use the job code snippet from the Starting The Scheduler section in the documentation because there is no cron on Windows. Windows is not officially supported for the task scheduler because of that and no instructions for it can be found in the documentation.
You could however get around the problem by creating a batch file, say scheduler.bat, that has the following contents:
cd c:\lamp\www\larasoft
php artisan schedule:run 1>> NUL 2>&1
Then you can add a Windows Scheduler Task to run that file every minute.
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 &
In your case
Run "where php.exe" in command prompt
Copy The php location
"paste\your\php\location" "artisan" InsertUser > "NUL" 2>&1 &

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