2016-12-01 27 views
1

I am following this tutorial毎分機能をスケジュールします。以下はlocalhost上の私のコードです。タスクスケジューリング:Laravel 5.3

class Kernel extends ConsoleKernel 
{ 
    protected $commands = [ 
     'App\Console\Commands\Inspire', 
    ]; 

    protected function schedule(Schedule $schedule) 
    { 
     $schedule->call($this->WriteFile())->everyMinute(); 
    } 

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

    private function WriteFile() { 
     $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
     $txt = "John Doe\n"; 
     fwrite($myfile, $txt); 
     fclose($myfile); 
    } 
} 

txtファイルに内容が表示されていないことがわかりました。私はパブリックフォルダにtxtファイルを配置しました。何か不足していますか?私は、この持っている私のKernal.php

Artisan::command('writeFile', function() { 
    Storage::disk('local')->put('file.txt', 'this is text !'); 
}); 

:私は、このコマンドを持っているファイルconsole.phpの私Console Routes

+0

は、あなたのサーバーへのCRONのエントリを追加したことがありますか? – istaro

+0

これはローカルホストに関するものです。 – Pankaj

答えて

2

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

をも、あなたが実行する必要があります。php artisan schedule:run

ファイルは次のようになりますファイルはパスに入ります:"/storage/app/file.txt"

あなたがファイルを読むことができる3210

Storage::get('file.txt');

が私のためにうまく働いていた。これは、あなたと一緒に働くことを願っています:)

+0

これは次のようになります: 'Artisan :: command( 'writefile'、function($ project){ Storage :: disk( 'local') - > put( 'newfile.txt'、$ txt); }) ; [詳細](https://laravel.com/docs/5.3/artisan) –

+0

あなたは 'php artisan schedule:run'を毎分実行するようにcronジョブを追加する必要があります –

+0

あなたが編集する必要がありますあなたの 'crontab'コマンドを使用して' crontab -e'コマンドを実行してから、このコマンドを追加してください。*/*/*/php/path/to/artisanスケジュール:run >>/dev/null 2>&1 この[ビデオ](https://www.youtube.com/watch?v=mp-XZm7INl8)cronジョブ –