简体   繁体   中英

Why my cron job laravel 5.3 not working on localhost?

I am using windows.

My code on \\app\\Console\\Kernel.php is like this:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    protected $commands = [
        Commands\CustomCommand::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('custom:command')
                 ->everyMinute();
    }

    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

My code on \\app\\Console\\Commands\\CustomCommand.php is like this:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class CustomCommand extends Command
{
    protected $signature = 'custom:command';

    protected $description = 'test cron job to update status on table order';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $id = 1;
        DB::table('orders')
          ->where('id', $id)
          ->update(['status' => 2, 'canceled_at' => date("Y-m-d H:i:s")]);
    }
}

I run php artisan list to see my cron job

After find my cron job (custom:command), then I run my cron job with like this : php artisan custom:command

It's successful update status = 2. After that I change the status manually again become 1, and then I wait one minute, it does not update status again

Is there anyone can help me?

您还应该在本地 Web 服务器上设置 cron 以运行 cron 作业。

How you change the status manually? You change it in the database? maybe the cron is running but 'id' int the database is equal to '2' and maybe this is why you dont see any change!

If you look for a better way to check if the Cron job is working

just add Log that will wroth to the log file

 public function handle()
{
    Log::info('Cron job working'); // you can also print variables
    $id = 1;
    DB::table('orders')
      ->where('id', $id)
      ->update(['status' => 2, 'canceled_at' => date("Y-m-d H:i:s")]);
}

Don't forget to had the Log to your source:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use Log; // Here

class CustomCommand extends Command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM