2017-06-13 174 views
0

这是我第一次设置Cron作业,但无法自动安排命令。Laravel cron作业未运行

所以我有一个命令:

public function handle() 
{ 
    $client = new Client(); 
    $crawler = $client->request('GET', 'http://www.oldham-chronicle.co.uk/news-features'); 
    $crawler->filter('div[id=content]>.homefeature')->each(function ($node, $key) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $image = $node->filter('img')->image(); 
     $image_s = $image->getUri(); 
     $filename = basename($image_s); 
     $image_path = ('news-gallery/' . $filename); 
     Image::make($image_s)->save(public_path('news-gallery/' . $filename)); 
     $id = 1+ $key + 1; 
     $news = News::where('id', $id)->first(); 
     // if news is null 
     if (!$news) { 
      $news = new News(); 
     } 
     $news->title = $title; 
     $news->datepublished = $datepublished; 
     $news->description = $description; 
     $news->link = $link_r; 
     $news->image = $image_path; 
     $news->save(); 
    }); 

    $crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $image = $node->filter('img')->image(); 
     $image_s = $image->getUri(); 
     $filename = basename($image_s); 
     $image_path = ('news-gallery/' . $filename); 
     Image::make($image_s)->save(public_path('news-gallery/' . $filename)); 
     $id = 1+ $key + 1; 
     $news = News::where('id', $id)->first(); 
     // if news is null 
     if (!$news) { 
      $news = new News(); 
     } 
     $news->title = $title; 
     $news->datepublished = $datepublished; 
     $news->description = $description; 
     $news->link = $link_r; 
     $news->image = $image_path; 
     $news->save(); 
     $this->info('Scraping done succesfully'); 
    }); 
} 

内核文件:

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

/** 
* Define the application's command schedule. 
* 
* @param \Illuminate\Console\Scheduling\Schedule $schedule 
* @return void 
*/ 
protected function schedule(Schedule $schedule) 
{ 
    $schedule->command('scrape:news') 
      ->everyMinutes(); 
      ->emailOutputTo('[email protected]); 
} 

我可以手动运行它作为PHP工匠刮:新闻和它的作品,但是当我将它添加到的crontab - e:

* * * * * php /var/www/fyproject/ schedule:run >> /dev/null 2>&1 

Cronjob永远不会运行,任何帮助!

+2

它应该是'PHP /无功/网络/ fyproject /工匠时间表:运行' – apokryfos

+0

哈哈很好的错误把它作为答案,我会标记它;) – Przemek

+0

没有笑话,我第一次尝试设置它也犯了完全相同的错误。 – apokryfos

回答

2

的命令需要通过artisan被调用,所以你一定要记得将它添加呼叫:

用途:

crontab -e * * * * * php /var/www/fyproject/artisan schedule:run >> /dev/null 2>&1