Search code examples
phplaravellaravel-5cronlaravel-artisan

Configure Laravel task scheduling with Crontab


I've create a laravel task and added to my crontab file


app/Console/Kernel.php

<?php

namespace App\Console;

use Carbon;

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\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $now = Carbon\Carbon::now('America/New_York');
        $dt = Carbon\Carbon::parse($now);
        $time_start = $dt->toTimeString();
        $dt = str_replace('-','_',$dt);
        $dt = str_replace(' ','_',$dt);
        $dt = str_replace('/',':',$dt);
        $schedule->exec('curl '.env('APP_URL').'fbwifi/acl_update')->everyMinute()
            ->sendOutputTo(public_path().'/tasks/log_'.$dt.'.txt');
    }
}

crontab file

*       *       *       *       *       /usr/local/bin/php artisan schedule:run
*       *       *       *       *       php artisan schedule:run

Result

For some reasons my crontab doesn't trigger.

After 5 minutes, I don't see anything generated in my public/tasks/ folder.

I even tried to put the full path to my php.

Am I missing anything? How would one go about debugging this?


Solution

  • In your crontab you need to set the path to point to the artisan file in your project's root directory.

    * * * * * php /path/to/project/artisan schedule:run

    If you're having trouble finding the absolute path to your project route then open terminal, cd into your project's route directory and then use the pwd command, which will give you the absolute directory.

    So for example:

    $ cd MyUser/dev/project
    $ pwd
    

    Will output something like

    /Users/MyUser/dev/project

    Then your cronjob would look like

    * * * * * php /Users/MyUser/dev/project/artisan schedule:run

    Also try restarting your cron daemon. I've found that helps sometimes when things don't run for me.