这是我第一次设置 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 = [
'AppConsoleCommandsNewsScrape'
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('scrape:news')
->hourly();
}
我转到一个项目文件夹,键入 crontab -e,我得到:
"crontab"不被识别为内部或外部命令,可操作的程序或批处理文件。
如何解决这个问题?请记住,这是我第一次使用它
在生产中,您必须设置以下 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。
没有 crontab 命令,这适用于您的生产环境,但在 Windows 上则不行。
等效项在命令行中,键入 at /?
以获得一些帮助,我无法帮助您,因为我从未使用过它