为什么我的 Bottle 服务器在启动时失败?
Why does my bottle server fail at startup?
我已经使用 python 和 bottle 以及一个 systemd 单元编写了一个小服务器,将其作为服务安装在 xubuntu 上。 python 脚本按预期工作,如果我从命令行 (sudo systemctl advairMeter
) 启动它,该服务工作正常,但它在系统启动时失败。
这是我的 .service 文件:
saul@linuxBox:~/Desktop$ cat /etc/systemd/system/advairMeter.service
[Unit]
Description=Advair meter
After=network.target
[Service]
ExecStart=/usr/bin/python3 server.py
Restart=always
WorkingDirectory=/home/saul/Projects/PythonProjects/medSched/
[Install]
WantedBy=multi-user.target
这是重启时发生的情况:
Apr 09 08:43:49 linuxBox systemd[1]: Started Advair meter.
Apr 09 08:43:49 linuxBox python3[1176]: Bottle v0.12.19 server starting up (using WSGIRefServer())...
Apr 09 08:43:49 linuxBox python3[1176]: Listening on http://192.168.1.103:8080/
Apr 09 08:43:49 linuxBox python3[1176]: Hit Ctrl-C to quit.
Apr 09 08:43:50 linuxBox python3[1176]: Traceback (most recent call last):
Apr 09 08:43:50 linuxBox python3[1176]: File "server.py", line 95, in <module>
Apr 09 08:43:50 linuxBox python3[1176]: run(host='192.168.1.103', port=8080)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/local/lib/python3.8/dist-packages/bottle.py", line 3137, in run
Apr 09 08:43:50 linuxBox python3[1176]: server.run(app)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/local/lib/python3.8/dist-packages/bottle.py", line 2789, in run
Apr 09 08:43:50 linuxBox python3[1176]: srv = make_server(self.host, self.port, app, server_cls, handler_cls)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/wsgiref/simple_server.py", line 154, in make_server
Apr 09 08:43:50 linuxBox python3[1176]: server = server_class((host, port), handler_class)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/socketserver.py", line 452, in __init__
Apr 09 08:43:50 linuxBox python3[1176]: self.server_bind()
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/wsgiref/simple_server.py", line 50, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: HTTPServer.server_bind(self)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/http/server.py", line 138, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: socketserver.TCPServer.server_bind(self)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/socketserver.py", line 466, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: self.socket.bind(self.server_address)
Apr 09 08:43:50 linuxBox python3[1176]: OSError: [Errno 99] Cannot assign requested address
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Main process exited, code=exited, status=1/FAILURE
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Failed with result 'exit-code'.
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Scheduled restart job, restart counter is at 4.
Apr 09 08:43:50 linuxBox systemd[1]: Stopped Advair meter.
现在是上面journalctl输出的第一部分,
Bottle v0.12.19 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.103:5678/
Hit Ctrl-C to quit.
是我看到的,如果我只是 运行 带有 python3 server.py
的 python 脚本,所以看起来脚本实际执行了,然后地址可以'没有被分配,因为已经有一个套接字在使用它,但是为什么重启完成后没有进程监听 post?
python脚本本身很简单,
<SNIP>
@route('/')
@auth_basic(is_authenticated_user)
def home():
<SNIP>
run(host='192.168.1.103', port=8080)
我假设错误出在我的 .service 文件中,但我无法在网上找到任何告诉我如何修复它的信息。这是我第一次同时使用 bottle 和 systemd,所以我可能会犯一个非常简单的错误。
您的服务很可能 运行 在您的网络在启动期间设置之前。这意味着您的 OS 没有任何可侦听的网络接口,因此您的脚本将失败。
要解决此问题,请将 [Unit]
部分更改为:
[Unit]
Description=Advair meter
Wants=network-online.target
After=network.target network-online.target
您可以查看 Cause a script to execute after networking has started? 了解更多详情。
我已经使用 python 和 bottle 以及一个 systemd 单元编写了一个小服务器,将其作为服务安装在 xubuntu 上。 python 脚本按预期工作,如果我从命令行 (sudo systemctl advairMeter
) 启动它,该服务工作正常,但它在系统启动时失败。
这是我的 .service 文件:
saul@linuxBox:~/Desktop$ cat /etc/systemd/system/advairMeter.service
[Unit]
Description=Advair meter
After=network.target
[Service]
ExecStart=/usr/bin/python3 server.py
Restart=always
WorkingDirectory=/home/saul/Projects/PythonProjects/medSched/
[Install]
WantedBy=multi-user.target
这是重启时发生的情况:
Apr 09 08:43:49 linuxBox systemd[1]: Started Advair meter.
Apr 09 08:43:49 linuxBox python3[1176]: Bottle v0.12.19 server starting up (using WSGIRefServer())...
Apr 09 08:43:49 linuxBox python3[1176]: Listening on http://192.168.1.103:8080/
Apr 09 08:43:49 linuxBox python3[1176]: Hit Ctrl-C to quit.
Apr 09 08:43:50 linuxBox python3[1176]: Traceback (most recent call last):
Apr 09 08:43:50 linuxBox python3[1176]: File "server.py", line 95, in <module>
Apr 09 08:43:50 linuxBox python3[1176]: run(host='192.168.1.103', port=8080)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/local/lib/python3.8/dist-packages/bottle.py", line 3137, in run
Apr 09 08:43:50 linuxBox python3[1176]: server.run(app)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/local/lib/python3.8/dist-packages/bottle.py", line 2789, in run
Apr 09 08:43:50 linuxBox python3[1176]: srv = make_server(self.host, self.port, app, server_cls, handler_cls)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/wsgiref/simple_server.py", line 154, in make_server
Apr 09 08:43:50 linuxBox python3[1176]: server = server_class((host, port), handler_class)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/socketserver.py", line 452, in __init__
Apr 09 08:43:50 linuxBox python3[1176]: self.server_bind()
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/wsgiref/simple_server.py", line 50, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: HTTPServer.server_bind(self)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/http/server.py", line 138, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: socketserver.TCPServer.server_bind(self)
Apr 09 08:43:50 linuxBox python3[1176]: File "/usr/lib/python3.8/socketserver.py", line 466, in server_bind
Apr 09 08:43:50 linuxBox python3[1176]: self.socket.bind(self.server_address)
Apr 09 08:43:50 linuxBox python3[1176]: OSError: [Errno 99] Cannot assign requested address
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Main process exited, code=exited, status=1/FAILURE
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Failed with result 'exit-code'.
Apr 09 08:43:50 linuxBox systemd[1]: advairMeter.service: Scheduled restart job, restart counter is at 4.
Apr 09 08:43:50 linuxBox systemd[1]: Stopped Advair meter.
现在是上面journalctl输出的第一部分,
Bottle v0.12.19 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.103:5678/
Hit Ctrl-C to quit.
是我看到的,如果我只是 运行 带有 python3 server.py
的 python 脚本,所以看起来脚本实际执行了,然后地址可以'没有被分配,因为已经有一个套接字在使用它,但是为什么重启完成后没有进程监听 post?
python脚本本身很简单,
<SNIP>
@route('/')
@auth_basic(is_authenticated_user)
def home():
<SNIP>
run(host='192.168.1.103', port=8080)
我假设错误出在我的 .service 文件中,但我无法在网上找到任何告诉我如何修复它的信息。这是我第一次同时使用 bottle 和 systemd,所以我可能会犯一个非常简单的错误。
您的服务很可能 运行 在您的网络在启动期间设置之前。这意味着您的 OS 没有任何可侦听的网络接口,因此您的脚本将失败。
要解决此问题,请将 [Unit]
部分更改为:
[Unit]
Description=Advair meter
Wants=network-online.target
After=network.target network-online.target
您可以查看 Cause a script to execute after networking has started? 了解更多详情。