【问题标题】:Running Python script as systemd service, connecting to InfluxDB results in ConnectionError将 Python 脚本作为 systemd 服务运行,连接到 InfluxDB 会导致 ConnectionError
【发布时间】:2019-04-24 15:06:44
【问题描述】:

我正在运行一个 shell 脚本作为 Ubuntu 的 systemd 服务,以便在启动时启动。该脚本在内部执行连接到 InfluxDB(通过 Python 的 influxdb 包)的 Python 脚本 (python_simulator.py)。

Python 脚本无法在启动时启动,检查日志表明这是因为连接到 InfluxDB 时出现“ConnectionError”。我将其解释为,在 Python 服务在启动时激活时,influxdb 服务可能尚未启动。因此,我尝试通过将“After”和“Wants”添加为“influxdb.service”来在服务中添加订单依赖项,从而激活在 influxdb 服务之后几秒钟的 Python 服务。但是,我仍然遇到同样的连接错误。

systemd 服务 (myservice.service) 如下所示:

[Unit]
Description= Python startup service.
After=influxdb.service
Wants=influxdb.service

[Service]
Type=forking
ExecStart=/bin/bash /home/test_user/Deploy/start.sh
ExecStop=/bin/bash /home/test_user/Deploy/stop.sh

[Install]
WantedBy=multi-user.target

Python脚本的日志文件(python_simulator.py)

Traceback (most recent call last):
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/util/connection.py", line 80, in create_connection
    raise err
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/util/connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connection.py", line 181, in connect
    conn = self._new_conn()
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connection.py", line 168, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f817d91b400>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8086): Max retries exceeded with url: /query?q=SHOW+DATABASES (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f817d91b400>: Failed to establish a new connection: [$

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "python_simulator.py", line 467, in <module>
    main(host=args.host, port=args.port)
  File "python_simulator.py", line 312, in main
    for db_dict in client.get_list_database():
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/influxdb/client.py", line 570, in get_list_database
    return list(self.query("SHOW DATABASES").get_points())
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/influxdb/client.py", line 416, in query
    expected_response_code=expected_response_code
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/influxdb/client.py", line 267, in request
    timeout=self._timeout
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/test_user/Deploy/py_venv/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8086): Max retries exceeded with url: /query?q=SHOW+DATABASES (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f817d91b400>: Failed to establish a new connection$

最后,阻塞的守护进程树显示 myservice.serviceinfluxdb.service 之后执行:

myservice.service +6.872s
└─influxdb.service @11.344s
  └─network-online.target @11.337s
    └─NetworkManager-wait-online.service @4.706s +6.630s
      └─NetworkManager.service @3.940s +674ms
        └─dbus.service @3.914s
          └─basic.target @3.728s
            └─sockets.target @3.728s
              └─snapd.socket @3.722s +5ms
                └─sysinit.target @3.712s
                  └─apparmor.service @3.276s +435ms
                    └─local-fs.target @3.266s
                      └─run-user-1000.mount @49.841s
                        └─swap.target @3.160s
                          └─dev-disk-by\x2duuid-16e1b46a\x2d79fc\x2d4965\x2d9932\x2d8f589e9e7057.swap @3.132s +23ms
                            └─dev-disk-by\x2duuid-16e1b46a\x2d79fc\x2d4965\x2d9932\x2d8f589e9e7057.device @3.130s

我不确定为什么我仍然无法使用 influxdb 执行脚本 (python_simulator.py)。还有其他依赖吗? myservice.service 中是否需要任何更改?任何帮助将不胜感激。

编辑 1:

原因可能是 ConnectionRefusedError 而不是 ConnectionError 这可能是因为当它连接到 Port 8086 流入时,什么都没有在听吗?如果是这样,我怎样才能把它放在依赖顺序?

【问题讨论】:

    标签: python systemd influxdb influxdb-python system-services


    【解决方案1】:

    即使您创建了服务依赖项,您仍然可能会遇到问题(因为 influxdb 可能需要一段时间才能准备好接受连接,在此期间 Python 代码可能会启动)。解决方案是:

    • 编写 Python 代码以在连接被拒绝或连接时重试
    • 配置 systemd 以在服务失败时重新启动您的服务。

    如果没有看到您的 Python 代码,很难建议第一个解决方案可能是什么样的,但是将您的设备配置为在失败时重新启动就像将以下内容添加到您的 [Service] 部分一样简单:

    Restart=on-failure
    

    您可以通过向您的 systemd 单元添加一个小的 shell 脚本来延迟 Python 代码的启动,直到 influxdb 准备好:

    ExecStartPre=/bin/sh -c 'while ! curl -sf http://localhost:8086/ping; do sleep 1; done'
    

    这将无限循环(从而阻止服务启动),直到 influxdb 成功响应 /ping 端点。

    【讨论】:

    • 作为 systemd 服务运行的 shell 脚本执行 Python 脚本和其他一些东西。即使 python 脚本失败,它的状态也始终是活动(运行)。是否可以让 systemd 服务等到端口 8086 出现?
    • 我已经为答案添加了一种可能的解决方案,尽管我认为前两个建议中的任何一个都会更好(因为如果在 Python 代码已经运行后必须重新启动 influxdb 服务会发生什么情况?)
    • 我同意将 systemd 配置为在失败时重新启动是一个更好的解决方案。但正如我所说,执行 .py 的错误不会导致 .sh 服务失败,因此它不会重新启动。
    • 虽然,等待端口 8086 的解决方案效果很好。为了检查如果 influxdb 停止会发生什么,它实际上也停止了 myservice 。我假设这是因为我在 myservice.service 下将 Wants 作为 influxdb.service。不过,我希望它能够重新启动,因为我已经输入了“Restart=on-failure”(可能是因为它已经死了并且没有失败)
    • 谢谢。但是(我还没有“深入”调查)这对我不起作用,直到我将TimeoutSec=360 添加到单元中以及 ping 中。另见:unix.stackexchange.com/questions/227017/…
    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2021-06-21
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多