使用PM2运行Node.js+Puppeteer启动过多浏览器报错求助
问题描述
在无头Ubuntu服务器上,用PM2运行Node.js应用,通过Puppeteer启动浏览器。启动80-85个浏览器后,无法创建新实例,应用崩溃。已确认服务器内存和磁盘空间充足,核心报错为pm2-root.service is not a snap cgroup,不清楚该错误含义。
浏览器启动代码
.... const options = { executablePath: '/snap/bin/chromium', defaultViewport: null, headless: true, args: ['--start-fullscreen', "--disable-features=IsolateOrigins","--disable-setuid-sandbox", "--disable-site-isolation-trials", "--disable-features=BlockInsecurePrivateNetworkRequests", "--disable-web-security", '--ignore-certificate-errors', '--ignore-certificate-errors-spki-list '] } options.args.push('--no-sandbox'); await puppeteer.launch(options); ....
系统信息
uname -a Linux pot-n7 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux nano /etc/os-release PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy apt list snapd -a Listing... Done snapd/jammy-updates 2.58+22.04 amd64 [upgradable from: 2.57.5+22.04ubuntu0.1] snapd/jammy-security,now 2.57.5+22.04ubuntu0.1 amd64 [installed,upgradable to: 2.58+22.04] snapd/jammy 2.55.3+22.04 amd64 snap --version snap 2.58.3 snapd 2.58.3 series 16 ubuntu 22.04 kernel 5.15.0-52-generic snap list core error: no matching snaps installed
报错信息
核心启动报错
EROROR Error: Failed to launch the browser process! 0|npm | /system.slice/pm2-root.service is not a snap cgroup 0|npm | TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md 0|npm | at onClose (/var/www/twitch-client/node_modules/puppeteer/src/node/BrowserRunner.ts:327:9) 0|npm | at Interface.<anonymous> (/var/www/twitch-client/node_modules/puppeteer/src/node/BrowserRunner.ts:313:16) 0|npm | at Interface.emit (node:events:525:35) 0|npm | at Interface.close (node:readline:590:8) 0|npm | at Socket.onend (node:readline:280:10) 0|npm | at Socket.emit (node:events:525:35) 0|npm | at endReadableNT (node:internal/streams/readable:1358:12) 0|npm | at processTicksAndRejections (node:internal/process/task_queues:83:21)
后续连锁报错
0|npm | TypeError: Cannot read properties of undefined (reading 'pages') 0|npm | at /var/www/twitch-client/src/autoBrowser.service.ts:98:47 0|npm | at runMicrotasks (<anonymous>) 0|npm | at processTicksAndRejections (node:internal/process/task_queues:96:5)
解决思路
错误原因解析:你使用的是Snap安装的Chromium,Snap应用默认会在专属的cgroup(控制组)中运行,而PM2进程属于
system.slice/pm2-root.service这个非Snap的cgroup。当启动大量实例后,Snap的资源隔离机制触发,阻止Chromium在非Snap cgroup中创建新进程,从而抛出该错误。替换Chromium安装方式:放弃Snap版Chromium,改用apt直接安装,避开Snap的cgroup限制:
sudo snap remove chromium sudo apt update && sudo apt install chromium-browser之后修改代码中的
executablePath为/usr/bin/chromium-browser。调整系统资源限制:80+浏览器实例本身会占用大量系统资源,即使内存充足,也可能触发进程数或文件句柄限制:
- 临时调整进程数限制:
ulimit -u 65535,永久修改需编辑/etc/security/limits.conf,添加* soft nproc 65535和* hard nproc 65535。 - 临时调整文件句柄限制:
ulimit -n 65535,永久修改需编辑/etc/security/limits.conf(添加* soft nofile 65535、* hard nofile 65535)和/etc/sysctl.conf(添加fs.file-max = 655350),执行sysctl -p生效。
- 临时调整进程数限制:
优化实例管理逻辑:尽量复用浏览器实例,用一个浏览器打开多个页面,而非每次新建浏览器进程,从根源减少资源消耗。
修复连锁报错:
Cannot read properties of undefined (reading 'pages')是因为浏览器启动失败导致browser对象未初始化,需在代码中添加启动失败的捕获逻辑,避免后续代码调用未定义的对象。
内容的提问来源于stack exchange,提问作者Kamuran Sönecek
相关产品推荐
相关产品推荐

