Laravel Scheduler on cPanel shared hosting

Laravel Scheduler on cPanel shared hosting

Laravel Scheduler is a great feature to run our desired task or take application action in a certain time or do a repeated task in a time interval. Suppose we have temp table and we have to clear table records in every 5 minutes or backup database every day once or clear logs every 30 minutes and etc. In this scenario, Laravel scheduler is a great option to do that. In this post, I'll show you a clean and step by step process on how to run a Laravel schedule on cPanel shared hosting with cPanel cronjob.

According to Laravel documentation, we have to run artisan command for laravel schedule like php artisan schedule:run but most of cPanel shared hosting provider disables the SSH option for their hosting security. In this situation, we can do that by cPanel cronjob. Don't panic, after reading this post, It'll be crystal clear to you about Laravel scheduler on cPanel shared hosting with a cronjob.

Cronjob

First, Let's know a little bit about Cronjob who are not familiar in this term, The cronjob is a handy feature of Linux hosting (cPanel) to automate a repetitive task. It can run specific action on your scheduled date and time.

 

Steps for run Laravel schedule on shared hosting

  1. Define a Laravel schedule task.
  2. Make a cronjob into your cPanel shared hosting.
  3. Test the laravel schedule is working or not.

 

1. Define a Laravel schedule task

Open the kernel.php file from app/Console directory and define a scheduled task into the schedule method.

protected function schedule(Schedule $schedule)
{

    $schedule->call(function () {

        // your schedule code
        Log::info('Working');
        
    })->everyMinute();

}

Look here I have called a closure in every minute. You can find more Laravel schedule option on Laravel official doc. Inside the closure body, you can do anything you want.

N.B: Some shared hosting company disables some functions for their hosting security issue like shell_exec, exec. If you are intending to do something related to these functions please make sure these functions are enabled by your hosting provider.

 

2. Make a cronjob into your cPanel shared hosting

In this step, we have to define cPanel cronjob for running our laravel schedule in every minute. For that, login into cPanel and goto Cron Jobs option then create a new cronjob like below.

  • Select Once Per Minute option from Common Settings dropdown box.
  • In the command text box put the value according to this format.
  • Format: /usr/local/bin/php /home/hosting_user_name/your_artisan_file_path schedule:run >> /dev/null 2>&1
  • After putting the command in the command text box, click the Add New Cron Job button.

Example:

add-laravel-cronjob.png

Change the red-underlined userla with your hosting username.

 

3. Test the laravel schedule is working or not

Before running a real code execution, test the Laravel Schedule with cPanel cronjob by logging some info. If the settings are done correctly and cronjob schedule triggered then you can put actual code what you intend to do.

In this case, I have placed a line Log::info('Working') for logging inside the schedule method of kernel.php for testing. So, I have found it's working perfectly and I am getting log into my log file.

[2020-02-14 04:20:01] local.INFO: Working
[2020-02-14 04:21:02] local.INFO: Working
[2020-02-14 04:22:01] local.INFO: Working  
[2020-02-14 04:23:02] local.INFO: Working
[2020-02-14 04:24:02] local.INFO: Working

 

Conclusion

Some of the laravel developers who deploy their Laravel application and claim that Laravel Schedule not working in cPanel shared hosting. I say It works and I have tested with proper settings. If you tried with proper settings then you will succeed to run Laravel schedule on shared hosting. If you face any trouble to do that then I'll say please check the cronjob command twice, most of the developers are not succeed for misconfiguring.

 

In this post, I have tried my best to give you a clear demonstration on how to run a laravel schedule task on cPanel shared hosting with proper explanation and example. If you find this post helpful to you then please share this post with others.


Share


Related Post


Laravel Livewire CRUD tutorial

Laravel 6 jQuery Ajax CRUD tutorial

Laravel 6 contact form tutorial

Laravel Macro - do more than laravel offer!

Laravel 6 Slack notification in real-time