Laravel 9升级后调度器重复执行命令问题求助
定时任务重复执行问题排查与解决
问题背景
将Laravel 8升级到Laravel 9,同时Ubuntu系统从16升级到20后,Kernel.php中配置的所有调度任务出现重复执行情况,导致数据库产生重复条目,且未对业务代码做任何修改。
排查信息
定时任务配置检查
通过crontab -e检查当前用户定时任务,仅配置了一条artisan schedule:run命令,但服务器进程显示存在多个任务实例。
Kernel.php调度代码
protected function schedule(Schedule $schedule) { //POD STATS update job $schedule->job(new PodStatsJob)->everyTenMinutes(); //Consignment table backup job #Pickup Email Reminder $pickup_reminder_time = substr(getSingleOrganisationSetting("pickup_reminder_time"), 0, 5) ?? '07:00'; // $schedule->command('conveyor:indexing')->dailyAt($conveyor_consignment_indexing); }
进程检查结果
执行ps aux | grep "artisan schedule:run"后,发现root和ubuntu两个用户都在运行相同的schedule:run命令,这是任务重复执行的核心原因:
root 1331614 0.0 0.0 2616 528 ? Ss 15:00 0:00 /bin/sh -c cd /var/www/html/coldxlogistics && php artisan schedule:run >> /dev/null 2>&1 root 1331616 0.0 0.9 216924 77220 ? S 15:00 0:03 php artisan schedule:run root 1345894 0.0 0.0 2616 592 ? Ss 17:39 0:00 /bin/sh -c cd /var/www/html/coldxlogistics && php artisan schedule:run >> /dev/null 2>&1 ubuntu 1345895 0.0 0.0 2616 600 ? Ss 17:39 0:00 /bin/sh -c cd /var/www/html/coldxlogistics && php artisan schedule:run >> /dev/null 2>&1 root 1345896 27.0 0.9 216924 79020 ? S 17:39 0:00 php artisan schedule:run ubuntu 1345897 28.0 0.9 216924 78944 ? S 17:39 0:00 php artisan schedule:run ubuntu 1345911 0.0 0.0 8484 2428 pts/0 S+ 17:39 0:00 grep --color=auto artisan schedule:run
解决步骤
清理重复的定时任务配置:
- 查看root用户的定时任务:
sudo crontab -u root -e - 查看ubuntu用户的定时任务:
crontab -e - 删除多余的
artisan schedule:run条目,仅保留一个(建议保留root用户配置,需确保项目目录对运行用户有足够权限)
- 查看root用户的定时任务:
终止残留进程:
- 杀掉所有正在运行的任务进程:
sudo pkill -f "php artisan schedule:run" - 等待crontab自动重启任务,或手动执行一次
cd /var/www/html/coldxlogistics && php artisan schedule:run测试
- 杀掉所有正在运行的任务进程:
验证修复效果:
- 等待下一个任务执行周期,检查数据库是否仍有重复条目
- 再次执行
ps aux | grep "artisan schedule:run",确认仅存在一个任务进程
额外排查点:
- 检查系统级定时任务目录(
/etc/cron.d/、/etc/cron.hourly/等),确保没有重复配置 - 确认系统升级过程中是否有备份恢复或自动脚本添加了重复任务
- 检查系统级定时任务目录(
内容的提问来源于stack exchange,提问作者web pakistan
相关产品推荐
相关产品推荐

