树莓派5上Cron运行Python+Selenium+Chromedriver报错求助
树莓派5 cron执行Selenium+Chromedriver脚本失败问题
环境与问题
- 树莓派5运行Debian Bookworm系统
- 因官方无ARM版Chromedriver,通过APT安装适配版本
- Python脚本在Geany编辑器及用户终端运行正常,但通过
crontab -e配置定时执行时出现启动失败错误
报错信息
Traceback (most recent call last): File "/home/dean/main_venv/luxpowerdl.py", line 69, in <module> driver = webdriver.Chrome(service=service) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__ super().__init__( File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 61, in __init__ super().__init__(command_executor=executor, options=options) File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__ self.start_session(capabilities) File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute self.error_handler.check_response(response) File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: #0 0x5555f50dc3e4 <unknown> #1 0x5555f4e11ac8 <unknown> #2 0x5555f4e40fac <unknown> #3 0x5555f4e3dc44 <unknown> #4 0x5555f4e7cf08 <unknown> #5 0x5555f4e48d74 <unknown> #6 0x5555f4e49760 <unknown> #7 0x5555f50a6a74 <unknown> #8 0x5555f50a9e04 <unknown> #9 0x5555f5096d7c <unknown> #10 0x5555f50aa714 <unknown> #11 0x5555f507f0e0 <unknown> #12 0x5555f50cbbc4 <unknown> #13 0x5555f50cbdc0 <unknown> #14 0x5555f50db5b4 <unknown> #15 0x7fff46fcee58 <unknown> #16 0x7fff47037f9c <unknown>
解决步骤
1. 配置Selenium无界面运行参数
cron运行环境没有图形界面,必须为Chromium添加headless及必要的启动参数,修改脚本中初始化driver的代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service # 配置Chromium启动选项 options = Options() options.add_argument('--headless=new') # 启用新版无界面模式 options.add_argument('--no-sandbox') # 禁用沙箱,cron环境下必须 options.add_argument('--disable-dev-shm-usage') # 避免/dev/shm内存不足问题 options.add_argument('--remote-debugging-port=9222') # 解决DevToolsActivePort缺失问题 options.binary_location = '/usr/bin/chromium-browser' # 指定Chromium可执行文件路径 # 初始化driver service = Service(executable_path='/usr/bin/chromedriver') driver = webdriver.Chrome(service=service, options=options)
2. 完善cron环境变量配置
cron默认的环境变量PATH比用户终端少,需手动指定完整路径及shell环境:
- 执行
crontab -e打开定时任务编辑器 - 添加以下内容(替换为你的脚本路径和执行时间):
SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games # 示例:每小时执行一次脚本,并输出日志到指定文件 0 */1 * * * /home/dean/main_venv/bin/python3 /home/dean/main_venv/luxpowerdl.py >> /home/dean/cron_script.log 2>&1
SHELL指定使用bash,避免默认sh导致的环境差异PATH包含所有必要的执行路径,确保chromedriver和Python能被找到- 日志输出便于后续排查问题
3. 补充显示环境变量(可选)
如果上述步骤仍未解决,可尝试在cron任务中添加DISPLAY=:0:
0 */1 * * * DISPLAY=:0 /home/dean/main_venv/bin/python3 /home/dean/main_venv/luxpowerdl.py >> /home/dean/cron_script.log 2>&1
4. 测试验证
- 保存crontab后,手动运行脚本测试是否正常:
/home/dean/main_venv/bin/python3 /home/dean/main_venv/luxpowerdl.py
- 查看日志文件确认执行情况:
cat /home/dean/cron_script.log
内容的提问来源于stack exchange,提问作者Peter_Brown_USA
相关产品推荐
相关产品推荐

