You need to enable JavaScript to run this app. Python Firestore脚本手动运行正常,crontab执行报503 DNS解析失败问题
优惠活动
大模型
产品
解决方案
定价
更多

Python Firestore脚本手动运行正常,crontab执行报503 DNS解析失败问题

问题现象

通过crontab定时执行Firestore写入Python脚本时,出现域名解析失败异常,手动在终端执行相同脚本无任何问题。
对应的数据库写入核心代码:

db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))

手动终端执行可正常完成写入,crontab触发时抛出如下堆栈:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/axatel/angel_bridge/esportazione_firebase/main.py", line 23, in <module>
    dato.getDati(dato, db, cursor, cursor2, fdb, select, anagrafica)
  File "/home/axatel/angel_bridge/esportazione_firebase/dati.py", line 19, in getDati 
db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/document.py", line 234, in set
    write_results = batch.commit()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/batch.py", line 147, in commit
    metadata=self._client._rpc_metadata,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/gapic/firestore_client.py", line 1121, in commit
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
    on_error=on_error,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 DNS resolution failed for service: firestore.googleapis.com:443

异常根因

crontab的执行环境为最小化隔离环境,和用户登录终端的shell环境存在差异,不会自动加载用户目录下的.bashrc、.bash_profile等配置文件,经常出现DNS解析规则、环境变量加载不全的问题,导致无法完成域名解析。

可落地修复步骤

  • 第一步:先验证crontab环境下的DNS解析能力,在crontab中添加临时测试任务:
    * * * * * nslookup firestore.googleapis.com > /tmp/cron_dns_test.log 2>&1
    
    等待任务执行完成后查看日志,确认是否存在解析失败的问题,排除真实网络连通性故障。
  • 第二步:修改crontab任务的执行方式,使用登录式shell启动脚本,让shell自动加载所有用户级环境配置。将原有crontab任务行从直接调用python的形式,修改为通过bash登录态执行的形式:
    * * * * * /bin/bash -lc "python3 /home/axatel/angel_bridge/esportazione_firebase/main.py > /tmp/cron_run.log 2>&1"
    
    其中-l参数代表让bash以登录shell模式启动,自动加载用户配置文件中的环境变量,包括DNS配置、Google服务凭证路径等。
  • 第三步:在crontab文件开头手动补充基础环境变量,避免最小化环境导致的依赖路径缺失:
    在crontab文件的最顶部添加如下配置:
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    SHELL=/bin/bash
    # 补充Google应用凭证路径(如果你的服务账号凭证是通过环境变量配置的)
    GOOGLE_APPLICATION_CREDENTIALS=/home/axatel/你的服务账号凭证文件路径.json
    
  • 第四步:如果测试发现crontab环境下依然存在DNS解析失败,检查系统/etc/resolv.conf配置,确认DNS服务器地址配置正确,没有被NetworkManager动态覆盖导致配置丢失;必要时可在/etc/hosts中添加firestore.googleapis.com的固定解析记录作为兜底。
  • 第五步:验证修复效果,将临时测试任务替换为正式的脚本任务,观察日志确认脚本可正常完成Firestore写入。

内容的提问来源于stack exchange,提问作者Patrik Penta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.08 07:27:00