简体   繁体   中英

why my cron job is not working in laravel?

in db i have column visit_clear i want it 0 after one day so i used this code in kernal.php

   <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        protected $commands = [
        ];
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('cron:update-user-not-new')->daily();
        }
    
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
    
            require base_path('routes/console.php');
        }
    }

and in command/UpdateUserNotNew.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\DB;

class UpdateUserNotNew extends Command
{
 
    protected $signature = 'cron:update-user-not-new';

    protected $description = 'Command description';



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

    public function handle()
    {
        $dayAgo = 1; // Days ago
        $dayToCheck = \Carbon\Carbon::now()->subDays($dayAgo)->format('Y-m-d');
        Customer::whereDate('visit_date', '<=', $dayToCheck)
        ->update([
            'visit_clear' => 0
        ]);
    }
}

i am sheduling commnd like this as u can see cron:update-user-not-new should i use crone:UpdateUserNotNew ?

You need to register your command in Kernel.php like this:

protected $commands = [
    'App\Console\Commands\UpdateUserNotNew',
];

You should then be able to run the command manually with php artisan cron:update-user-not-new

In order for the automatic running of the command to work, you need to add an entry to your system's task scheduler, as this is what Laravel uses to run commands on a schedule.

Assuming you are using Linux, you need to add an entry to your crontab. To do this, in a command prompt enter crontab -e , hit enter, and add this line:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Don't forget to replace /path-to-your-project with the root folder of your project Once done editing the crontab, save and close the editor and the new entries should be installed, and your command should now run on the schedule.

All this info came from https://laravel.com/docs/7.x/scheduling so if you need more info take a look there

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