lundi 4 janvier 2016

Laravel 5.1 Scheduler is not Working automatically?

For the sake of testing and learning the Schedule and Cron functionality of Laravel I have created an custom artisan command sms:birthday with it's calss HappyBirthday.

The handle method of this Class only append a given text file with some text.

This sms:birthday custom artisan command does work and append a text file each time when the command is run like following:

php artisan sms:birthday

I then wanted to use the cron job scheduling of Laravel 5.1. I included this command in "kernel.php" file using the following command:

php /var/www/myLaravel5Project/artisan schedule:run >> /dev/null 2>&1

Whenever I run the above command it works fine and my file get's appended. But it dose not append my file every minute (but it should be) which means cron is not working. I don't know what's wrong with this.

Here is my kernerl.php file:

<?php

   namespace App\Console;

   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\HappyBirthday',
       ];

       /**
        * 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('sms:birthday')->everyMinute();
       }
   }

Here it is my HappyBirthday Class:

    <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Mail;
use File;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends a Happy birthday message to users via SMS.';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = '/home/user/Desktop/cron.txt';
        $contents = "USER\n";
        $bytes_written = File::append($file, $contents);
    }
}

Please help me in this regard that what's this how to run the sms:birthday command everyMinute automatically??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire