Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run laravel 5.2 artisan command on cron job on hostgator hosting server cpanel?

I have created an example schedule job to run after a specific time in Laravel 5.2. And this is working fine on localhost through artisan command.

I am running this command on local server: php artisan Demo:Cron

Now I am adding this task in cPanel's advance option cron job on HostGator hosting server. But it is not working.

This is the command I am trying:

cd /home/pofindia/public_html/beta-var1/ && /usr/local/bin/php artisan Demo:Cron

Php version: 5.4 default. And here is my example file

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('items')->insert(['name'=>'hello new']);
        $this->info('Demo:Cron Cummand Run successfully!');
    }
}
like image 982
Sweety Avatar asked Aug 06 '16 12:08

Sweety


People also ask

How do I run a cron job in laravel cPanel?

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. After putting the command in the command text box, click the Add New Cron Job button.


1 Answers

run

which php

to see where the php executable is, copy that path.

crontab -e

(I'm assuming this is your project root where your artisan is located /home/pofindia/public_html/beta-var1/ but this requires you to add artisan to it, as so /home/pofindia/public_html/beta-var1/artisan)

add the crontab entry

* * * * * /usr/local/bin/php  /home/pofindia/public_html/beta-var1/artisan Demo:Cron

really all you are doing here is providing the absolute paths

like image 101
chrismillah Avatar answered Sep 19 '22 02:09

chrismillah