Setting up a Laravel cron job in cPanel
Asked Answered
U

5

10

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?

Unassailable answered 30/6, 2017 at 20:10 Comment(0)
M
8

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.

Monmouthshire answered 30/6, 2017 at 20:24 Comment(5)
thanks Pascal for the info. Any idea why the cron job doesn't run? I used it as described php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1Unassailable
First you can debug this by executing the command manually, ex: php /abolute-path-to-your-app/artisan schedule:run and see if there is any error at this point.Monmouthshire
hi Pascal it works now. I found out that /dev/null 2>&1' is used in cron job when you do not want to send email when cron job runs. So, what the command should be exactly to get emails from cpanel when it runs the cron job?Unassailable
You have to remove the >> /dev/null 2>&1 part and set the MAILTO to your email. I think there is an interface to do this in cPanel, but make sure to remove the part I said.Monmouthshire
/usr/local/bin/php /home/i198yhcgeee/public_html/artisan schedule:run >> /dev/null 2>&1Iliad
Y
12

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
Yakut answered 30/10, 2020 at 15:2 Comment(0)
M
8

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.

Monmouthshire answered 30/6, 2017 at 20:24 Comment(5)
thanks Pascal for the info. Any idea why the cron job doesn't run? I used it as described php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1Unassailable
First you can debug this by executing the command manually, ex: php /abolute-path-to-your-app/artisan schedule:run and see if there is any error at this point.Monmouthshire
hi Pascal it works now. I found out that /dev/null 2>&1' is used in cron job when you do not want to send email when cron job runs. So, what the command should be exactly to get emails from cpanel when it runs the cron job?Unassailable
You have to remove the >> /dev/null 2>&1 part and set the MAILTO to your email. I think there is an interface to do this in cPanel, but make sure to remove the part I said.Monmouthshire
/usr/local/bin/php /home/i198yhcgeee/public_html/artisan schedule:run >> /dev/null 2>&1Iliad
C
5

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
Carl answered 18/12, 2020 at 23:50 Comment(0)
L
4

This worked for me

/usr/local/bin/php /home2/maildoll/demo.maildoll.com/artisan queue:work --stop-when-empty >> /dev/null

enter image description here

Lympho answered 20/5, 2021 at 6:44 Comment(0)
P
0

Try this one. We have to put '&&' in between the path and command. This worked for me

/usr/local/bin/php /home/hosting-username/laravel-folder/artisan &&  command:my_command >> /dev/null
Photoemission answered 4/8, 2023 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.