Setting up a Laravel cron job in cPanel

旧城冷巷雨未停 提交于 2021-01-03 01:31:13

问题


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?


回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/44854161/setting-up-a-laravel-cron-job-in-cpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!