我有硬件服务,需要作为服务运行。如果我直接运行 sudo harware_servicee.py 它是否按预期工作,但是如果我尝试与服务相同的操作,则不会创建 app.sock 文件。
给出下面的示例场景。
hardware_service.py
app = Flask(__name__)
@app.route("/overall")
def overall_stats_route():
data = {
"os": get_os_info(),
"cpu": get_cpu_info(),
"disk": get_disk_info(),
"swap": get_swap_info(),
"disk_partitions": get_partitions_info(),
}
return jsonify(data)
if __name__ == "__main__":
print("Running as service...")
app.run(host="unix://usr/bin/app.sock")
cd /etc/系统/系统 hardware_service.service
[Unit]
Description=My Service
[Service]
ExecStart=/usr/bin/python /home/<user>/hardware_service.py
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start hardware_service
sudo systemctl status hardware_service
sudo systemctl enable hardware_service
如果我将其作为服务(hardwaare_service)运行,app.sock 不会在 /usr/bin 下创建。但是如果运行 sudo python hardware_service.py 然后 app.sock 文件正在创建。
sudo systemctl status hardware_service
`● hardware_service.service - My Service
Loaded: loaded (/etc/systemd/system/hardware_service.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-02-23 09:39:51 UTC; 21h ago
Main PID: 22559 (hardware_servic)
CGroup: /system.slice/hardware_service.service
├─22559 /home/narenautod/dist/hardware_service
└─22560 /home/narenautod/dist/hardware_service
Feb 23 09:39:51 narenautod systemd[1]: Started My Service.
Feb 23 09:39:51 narenautod hardware_service[22559]: Running as service...
Feb 23 09:39:51 narenautod hardware_service[22559]: * Serving Flask app "hardware_service" (lazy loading)
Feb 23 09:39:51 narenautod hardware_service[22559]: * Environment: production
Feb 23 09:39:51 narenautod hardware_service[22559]: WARNING: This is a development server. Do not use it in a production deployment.
Feb 23 09:39:51 narenautod hardware_service[22559]: Use a production WSGI server instead.
Feb 23 09:39:51 narenautod hardware_service[22559]: * Debug mode: off
Feb 23 09:39:51 narenautod hardware_service[22559]: * Running on unix:////app.sock (Press CTRL+C to quit)`
Feb 23 11:01:49 narenautod systemd[1]:当前命令从单元文件中消失,命令列表的执行将不会恢复。
客户端.py
`import requests_unixsocket
session = requests_unixsocket.Session()
base_url = "http+unix:///usr/bin/app.sock"
r = session.get(base_url + "/overall")
print(r.status_code)
print(r.text)
assert r.status_code == 200`
python 客户端.py
`Traceback (most recent call last):
File "client.py", line 6, in <module>
r = session.get(base_url + "/overall")
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 498, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 486, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 598, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(2, 'No such file or directory'))`
我是否遗漏了将其作为服务运行的任何内容?
操作系统:centos