这是我第一次设置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')
->hourly();
}
然后我转到项目文件夹,键入crontab -e,然后我得到:
' crontab的'不被视为内部或外部命令, 可操作程序或批处理文件。
如何解决这个问题?请记住这是我第一次使用它
答案 0 :(得分:1)
在制作中,您必须设置以下crontab:
文档:https://laravel.com/docs/5.4/scheduling
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
请务必根据需要更新/path-to-your-project/
。
这将每分钟运行php artisan schedule:run
命令,检查是否有任何调度程序需要运行。
我不知道如何为Windows设置此功能,但我建议使用使用Homestead和Vagrant的开发环境Virtual Box。它将为您提供Ubuntu VM,因此您可以使用crontab。
答案 1 :(得分:0)
在Windows上没有crontab命令,这可以工作或生产环境,但不在Windows上。
等效于命令行,输入at /?
以获得一些帮助,我无法帮助你,因为我从未使用过它