How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project

Created at 19-Jul-2023 , By samar

You can create a command and schedule command in kernel.php file and create a cron job on live server to run that specific command in laravel project.

1. Create command in laravel\app\Console\Commands directory using php artisan command.

php artisan make:command LoginfoCommand

2. Make changes in file as per your requirments.

laravel\app\Console\Commands\LoginfoCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;

class LoginfoCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'log:run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run a log command';

    /**
     * Execute the console command.
     */
    public function handle()
    {

        Log::info('Laravel schedular run successfully!');
        
    }
}

3. Open kernel.php file and make changes as per below code.

laravel\app\Console\Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('log:run')->everyMinute();
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');
        require base_path('routes/console.php');
    }
}

4. Login to cpanel and search for cron jobs and paste below text to command input field and hit add new cron job button. Don't forgot to change your laravel project folder path as per your project directory structure.

/usr/local/bin/php /home/user/public_html/artisan log:run >/dev/null 2>&1

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.