2016-08-01 66 views
0

Cron上有一些时间表,需要将他们的输出保存在数据库中。现在可以将其保存到文件或发送给电子邮件。有可能以某种方式将它发送到一个URL或启动一个预先准备好的脚本? 我正在使用Laravel调度程序。这些命令类似于这个“* * * * * php/path/to/artisan schedule:run >>/dev/null 2”& 1“如何建议Cronjob将控制台输出重定向到url或脚本?

+0

请与你的问题更准确,我只是不明白你想做的事你有什么,什么...... – corentingi

回答

0

我需要得到一个laravel cron的生成输出计划并将其保存在数据库中。 Laravel允许将输出插入/追加到文件或通过电子邮件发送。 因此,解决方案是将输出插入到文件中,然后使用after钩子读取该文件,最后查询数据库。代码是波纹管。

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

protected $outputFile = 'app/schedules.txt'; 

/** 
* Define the application's command schedule. 
* 
* @param \Illuminate\Console\Scheduling\Schedule $schedule 
* @return void 
*/ 
protected function schedule(Schedule $schedule) 
{ 
    $schedule->command('ReportExpiringResponseDate')->weekly()->mondays()->wednesdays() 
     ->sendOutputTo(storage_path($this->outputFile)) 
     ->after(function() { 
      $this->saveLogs('ReportExpiringResponseDate'); 
     }); 
} 

protected function saveLogs($scheduleName) 
{ 
    $output = File::get(storage_path($this->outputFile)); 
    $logModel = (new ScheduleLog())->newInstance(); 
    $logData = [ 
     'schedule' => $scheduleName, 
     'schedule_output' => $output 
    ]; 
    if ($logModel->validate($logData)) { 
     $logModel->fill($logData); 
     $logModel->save(); 
    } else 
     echo 'invalid data for logs'; 
} 

}

相关问题