Flask+Selenium部署Nginx+Gunicorn后Chrome启动崩溃问题求助
Flask+Selenium部署后Chrome启动崩溃问题
我有一个基于Flask的应用,通过Selenium执行脚本接收POST请求并打开指定URL。本地直接运行Flask时一切正常,POST请求解析无问题,但通过Nginx、Gunicorn和systemd部署以实现7*24小时运行后,收到POST请求时日志出现以下错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed. (chrome not reachable) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
已确认Chrome、ChromeDriver、Selenium版本匹配:
chromedriver --version ChromeDriver 115.0.5790.170 google-chrome --version Google Chrome 115.0.5790.170 selenium Version: 4.11.2
Python文件配置
from flask import Flask, request, jsonify from flask_cors import CORS from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time import logging logging.basicConfig(filename='/home/username/logs.log', level=logging.INFO) app = Flask(__name__) CORS(app) def open_url_in_selenium(url): service = Service('/usr/local/bin/chromedriver') service.start() options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--disable-extensions') options.add_argument('--remote-debugging-port=9222') options.add_argument('--log-level=DEBUG') options.add_argument('--enable-logging') options.add_argument('--disable-software-rasterizer') options.binary_location = '/usr/bin/google-chrome' options.add_argument('--log-path=' + log_file_path) driver = webdriver.Chrome(options=options) driver.get(url) return driver
systemd服务配置
[Unit] Description=XYZ thing After=network.target [Service] User=username Group=username WorkingDirectory=/home/username ExecStart=/usr/bin/gunicorn --workers 1 --bind X.X.X.X:XXXX server:app Restart=always [Install] WantedBy=multi-user.target
另外,本地运行Flask时需要用sudo python3 filename.py才能避免权限拒绝,使用systemd时也用了sudo命令,但仍出现问题。请问这是否是权限问题?如何正确配置权限?以及该问题的原因是什么?
内容的提问来源于stack exchange,提问作者boring person
相关产品推荐
相关产品推荐

