Laravel调度执行sitemap:generate时sitemap.xml标签缺失问题求助
Laravel定时任务生成Sitemap页面数波动问题
环境与配置
- 已安装
spatie/laravel-sitemapComposer包 - 站点缓存由
spatie/laravel-responsecache负责
相关代码
Sitemap生成命令
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use Spatie\Crawler\Crawler; use Spatie\Sitemap\SitemapGenerator; class GenerateSitemap extends Command { ... protected $signature = 'sitemap:generate'; ... public function handle() { ...Here is logging... SitemapGenerator::create(config('app.url')) ->configureCrawler(function (Crawler $crawler) { $crawler->setUserAgent('Sitemaper'); }) ->writeToFile(public_path('sitemap.xml')); } }
调度器配置
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * Define the application's command schedule. */ protected function schedule(Schedule $schedule): void { $schedule->command('sitemap:generate')->hourly(); } }
Cron配置
* * * * * cd /var/www/projectname && php artisan schedule:run >> /dev/null 2>&1
问题现象
手动执行php artisan sitemap:generate时,可正常生成包含15个页面的sitemap.xml;但通过Cron调度执行时,生成的页面数在0到5之间波动,多数结果不一致。
已尝试操作
- 清除缓存
- 在Crawler实例和
.env文件中设置缓存绕过请求头 - 确认
.env中的APP_URL配置正确
补充说明
若将调度频率从hourly()改为everyMinute(),站点地图可正常生成,但每分钟生成会浪费服务器资源,并非可行解决方案。
内容的提问来源于stack exchange,提问作者Ezitis
相关产品推荐
相关产品推荐

