Search code examples
phplaravellaravel-5croncpanel

How to run artisan command schedule:run on hosting server? (Laravel)


I have a statusUpdate.php file in the xampp\htdocs\project\app\Console\Commands folder.

statusUpdate.php :

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;


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

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Update Job status daily';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $affected = DB::table('jobs')->update(array('status' => 1));
}
}

I created it following the official Laravel documentation. After that, I added \App\Console\Commands\statusUpdate::class to the Kernel.php file located in xampp\htdocs\project\app\Console.

Here's the code from the Kernel.php file:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\statusUpdate::class,
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('status:update')
             ->everyFiveMinutes();
}
}

I then ran the following command using CMD on Windows:

php artisan schedule:run

Now, it works fine on my local server. The status field in my jobs table updates correctly to 1.

However, when I deployed this project to shared hosting and set up a CRON job in cPanel, it didn't work. The CRON job command is:

php /path/to/artisan schedule:run 1>> /dev/null 2>&1

In this hosted environment, the command isn't functioning. How can I solve this issue?


Solution

  • Well. I am giving you the answer as per what you have said.

    Cron job command is like this : php /path/to/artisan schedule:run 1>> /dev/null 2>&1

    The path should be locating the artisan file in the server. Like this:

    Let's say your artisan file location is /var/www/artisan, then the simple answer could be do like this:

    php /var/www/artisan schedule:run 1>> /dev/null 2>&1

    Just check if that works. Thank You!

    UPDATE:

    https://i.sstatic.net/CYjI0.png

    This is how it should look like.