如何在WSL2中运行多个Laravel queue:work队列工作进程?
在WSL2中同时运行多个Laravel队列工作进程的方法
以下三种方案均可实现30个队列进程的模拟需求,你可以根据实际场景选择:
方案1:临时快速启动(适合单次测试场景)
直接通过shell循环在后台批量启动进程,无需额外安装工具:
- 执行命令启动30个后台队列进程:
for i in {1..30}; do php artisan queue:work --timeout=0 & done - 查看所有运行中的队列进程:
jobs - 批量停止所有队列进程:
pkill -f "php artisan queue:work"
方案2:Supervisor配置(推荐,完全匹配生产环境用法)
生产环境普遍使用Supervisor管理队列进程,用该方案模拟的效果最贴近真实生产表现:
- 先在WSL2中安装Supervisor:
sudo apt update && sudo apt install supervisor - 新增队列配置文件
/etc/supervisor/conf.d/laravel-queue.conf,填入以下内容,注意替换尖括号内的实际参数:
[program:laravel-queue] process_name=%(program_name)s_%(process_num)02d command=php <你的项目完整绝对路径>/artisan queue:work --timeout=0 autostart=true autorestart=true user=<你的WSL系统用户名> numprocs=30 redirect_stderr=true stdout_logfile=<你的项目完整绝对路径>/storage/logs/queue-worker.log stopwaitsecs=3600
- 加载配置并启动进程:
- 刷新Supervisor配置:
sudo supervisorctl reread && sudo supervisorctl update - 启动所有30个队列进程:
sudo supervisorctl start laravel-queue:* - 查看进程运行状态:
sudo supervisorctl status - 停止所有队列进程:
sudo supervisorctl stop laravel-queue:*
方案3:Laravel原生多进程参数(仅支持Laravel 9及以上版本)
如果你使用的Laravel版本≥9,可直接用官方内置的多进程参数启动,操作最简单:php artisan queue:work --timeout=0 --processes=30
注意:WSL2的性能上限和物理服务器有差异,测试时可通过
htop命令查看CPU、内存占用情况,避免进程过多导致系统卡顿。
内容的提问来源于stack exchange,提问作者Fil
相关产品推荐
相关产品推荐

