将Python脚本作为systemd服务运行,连接到XDB会导致ConnectionError
我正在运行一个shell脚本作为Ubuntu的systemd服务,在启动时启动。该脚本在内部执行一个Python脚本(Python_simulator.py),该脚本连接到InfluxDB(通过Python的InfluxDB包) Python脚本无法在启动时启动,检查日志表明这是因为在连接XDB时出现了“ConnectionError”。我将其解释为,在启动时Python服务激活时,XDB服务可能没有启动。因此,我尝试在服务中添加订单依赖项,方法是将“在之后”和“想要”添加为“influxdb.service”,它在influxdb服务几秒钟后激活Python服务。但是,我仍然得到相同的连接错误 systemd服务(myservice.service)看起来像:将Python脚本作为systemd服务运行,连接到XDB会导致ConnectionError,python,systemd,influxdb,influxdb-python,system-services,Python,Systemd,Influxdb,Influxdb Python,System Services,我正在运行一个shell脚本作为Ubuntu的systemd服务,在启动时启动。该脚本在内部执行一个Python脚本(Python_simulator.py),该脚本连接到InfluxDB(通过Python的InfluxDB包) Python脚本无法在启动时启动,检查日志表明这是因为在连接XDB时出现了“ConnectionError”。我将其解释为,在启动时Python服务激活时,XDB服务可能没有启动。因此,我尝试在服务中添加订单依赖项,方法是将“在之后”和“想要”添加为“influxdb.
[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\u 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$
我不确定为什么我仍然无法在XDB就绪的情况下执行脚本(python_simulator.py)。还有其他依赖项吗?myservice.service中是否需要任何更改?任何帮助都将不胜感激
编辑1:
原因可能是ConnectionRefusedError而不是ConnectionError,这可能是因为当它连接到8086端口的流入时,没有任何东西在监听它?如果是这样,我如何将其按依赖关系顺序排列?即使创建了服务依赖关系,也可能会遇到问题(因为XDB可能需要一段时间才能接受连接,在此期间Python代码可能会启动)。解决办法是:
- 编写Python代码,以便在连接被拒绝或中断时重试连接
- 配置systemd以在服务失败时重新启动服务
[Service]
部分即可:
Restart=on-failure
通过向systemd单元添加一个小的shell脚本,可以延迟Python代码的启动,直到XDB准备就绪:
ExecStartPre=/bin/sh -c 'while ! curl -sf http://localhost:8086/ping; do sleep 1; done'
这将无限期地循环(从而阻止服务启动),直到influxdb成功响应
/ping
端点。作为systemd服务运行的shell脚本执行Python脚本和其他一些操作。即使python脚本失败,其状态也始终为活动(正在运行)。有没有可能让systemd服务等到端口8086出现?我在答案中添加了一个可能的解决方案,尽管我认为前两个建议中的任何一个都会更好(因为如果在Python代码已经运行之后必须重新启动XDB服务,会发生什么情况?)我同意将systemd配置为在出现故障时重新启动是一个更好的解决方案。但正如我所说,执行.py时出错不会导致.sh服务失败,因此它不会重新启动。不过,等待端口8086的解决方案工作得很好。为了检查xdb停止时会发生什么,它实际上也停止了myservice。我假设这是因为我在myservice.service下有xdb.service。尽管如此,我还是希望它能像我所说的“restart=on failure”(可能是因为它“死了,没有失败”)那样重新启动,多亏了这一点。然而(我还没有深入调查过),在我将TimeoutSec=360
添加到装置和ping中之前,这对我来说是行不通的。另见:
ExecStartPre=/bin/sh -c 'while ! curl -sf http://localhost:8086/ping; do sleep 1; done'