0

I'm using Laravel 5.3.26 and cannot set the scheduler to run automatically although i have the cron job ready.

I' ve created a new command ligmena:update, below is the code:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class ligmena extends Command {

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ligmena:update';

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        //
        $today = strtotime("now");
        $energa_symvolaia = DB::table('symvolaia')->where('eidos_kinisis', '1')->get();
        foreach ($energa_symvolaia as $es) {
            $imerominia_lixis = strtotime(str_replace("/", "-", $es->imerominia_lixis));
            if ($today > $imerominia_lixis)
                DB::table('symvolaia')->where('id', '<', $es->id)->update(['eidos_kinisis' => '4']);
        }
    }

}
?>

Below is the code of Kernel.php

<?php

namespace App\Console;

use DB;
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\ligmena::class,
            //
    ];

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

        $schedule->command('ligmena:update')->everyMinute();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands() {
        require base_path('routes/console.php');
    }

}
?>

I've setup the cron job like this:

php /home/site.com/public_html/testdemo/artisan schedule:run >> /dev/null 2>&1

and it runs every minute.

If I run the command manually it works fine.

Any suggestions?

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
dip
  • 189
  • 6
  • 18
  • Please post the whole line in your cronjob file. – OptimusCrime Sep 22 '17 at 06:45
  • * * * * * * php /home/site.com/public_html/testdemo/artisan schedule:run >> /dev/null 2>&1 – dip Sep 22 '17 at 06:48
  • And if you copy the command `php /home/site.com/public_html/testdemo/artisan schedule:run` then it runs? It does look correct, compared to the documentation. – OptimusCrime Sep 22 '17 at 06:54
  • i run php artisan ligmena:update and it makes the update. – dip Sep 22 '17 at 06:56
  • Copy the exact command and check if it works. It should work just fine. What cronjob is doing is basically copying the command and running it itself. Only other thing I could think of is that you run the cronjob with wrong user (does not have access to either public_html or to do what you want), or that you should have `/bin/php` instead of `php` but I dont think that is likely. – OptimusCrime Sep 22 '17 at 06:58
  • No luck. I tested the cronjod with a sample php file and it works fine. But the laravel scheduler still doesnt run – dip Sep 22 '17 at 07:38
  • Does the command run if you do `php /home/site.com/public_html/testdemo/artisan ligmena:update` on its own? – apokryfos Sep 22 '17 at 07:47
  • Yes it runs. Locally (windows-Xampp) it makes the update. On server that is on Centos7 it runs but doesn't make any update as i see right now – dip Sep 22 '17 at 07:51
  • I've tested the cron job with another php file and it runs every minute. The code for update is ok as soon as i run it locally and it makes the update. Finally i think that the schedule runs and for some reason it cannot make the update. When i run the command on server is says: Running scheduled command: ligmena:update > 'dev/null' 2&.1 & but it doesn't make the update – dip Sep 22 '17 at 07:58
  • As i understand the issue comes up because i make a select on the database. $energa_symvolaia = DB::table('symvolaia')->where('eidos_kinisis', '1')->get(); While it runs it won't continue to run the code after this line. If i just make the update in the DB there is no issue. Are there any restrictions in the code that we can add? – dip Sep 22 '17 at 09:01

1 Answers1

0

I 've solved the issue using raw mysql. The cron job was running fine and the code was ok but it was not changing the DB. After i changed to raw mysql it worked fine

dip
  • 189
  • 6
  • 18