我有一个服务,我试图以这种方式运行,但它是一个稍微大一点的 python 程序。我退后一步,构建了一个简单的 Python 程序,看看我是否可以让它运行。当我尝试通过 telnet 连接到正在运行的此套接字时,它失败了。下面是 .socket、.service 和 .py 文件....
testPy.socket
[Unit]
Description=Socket to TESTPY for connection
PartOf=testPy.service
[Socket]
ListenStream=30001
[Install]
WantedBy=sockets.target
testPy.service
[Unit]
Description=TEST PY
After=network.target testPy.socket
Requires=testPy.socket
[Service]
ExecStart=/home/workers/miniconda2/bin/python /home/workers/testPy.py
StandardInput=socket
[Install]
WantedBy=default.target
testPy.py
import sys
END_OF_LINE = '\r\n'
while(1):
input = sys.stdin.readline()
buffer = input.strip()
if not buffer:
sys.stdout.write("OKAY DUDE")
sys.stdout.flush()
continue
if buffer in ['quit', 'QUIT']:
break
sys.stdout.write('\n' + buffer + END_OF_LINE)
sys.stdout.flush()
现在,如果我在命令行中运行它,它运行良好。我可以输入退出,它退出循环,回声任何东西..
如果我说:
systemctl start testPy.socket
然后输入:
远程登录本地主机 30001
它连接一点然后放下它。然后各种状态(对我来说)是非描述性的:
systemctl status testPy.socket
● testPy.socket - Socket to TESTPY for connection
Loaded: loaded (/etc/systemd/system/testPy.socket; disabled; vendor preset: disabled)
Active: failed (Result: service-failed-permanent) since Thu 2021-03-11 13:59:54 EST; 11min ago
Listen: [::]:30001 (Stream)
Mar 11 13:59:42 dhcp-093.apo.nmsu.edu systemd[1]: Listening on Socket to TESTPY for connection.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Unit testPy.socket entered failed state.
systemctl status testPy.service
● testPy.service - TEST PY
Loaded: loaded (/etc/systemd/system/testPy.service; disabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Thu 2021-03-11 13:59:54 EST; 12min ago
Process: 2087 ExecStart=/home/workers/miniconda2/bin/python /home/workers/testPy.py (code=exited, status=1/FAILURE)
Main PID: 2087 (code=exited, status=1/FAILURE)
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Started TEST PY.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service: main process exited, code=exited, status=1/FAILURE
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Unit testPy.service entered failed state.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service failed.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: start request repeated too quickly for testPy.service
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Failed to start TEST PY.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service failed.
我相信如果我可以让这个简单的测试工作,我可以获得我需要运行的更大的 .py 文件,因为它的工作原理基本相同。我为此构建了一个服务和套接字,通常有相同的错误。虽然systemctl status kosmos.service
给出了一个失败的仍然但是说主PID状态= 0/成功所以这很奇怪。
它说启动限制是失败的,但是如果像这里这样简单的服务必须启动并启动并启动这意味着其他错误,猜测我的套接字或服务文件中的配置但不确定是什么。我希望我的 python 不会因监听 sys.stdin.readline 等而改变,而它读取的行只是来自另一台机器在该端口(30001)上建立的连接。我认为这就是所有这些套接字的作用(所有这些都是因为它曾经在带有 xinetd 的旧机器上运行)
我假设你已经完成了
systemctl enable testPy.socket
和systemctl start testPy.socket
步骤?我发现的一件事我还不太明白,那testPy.service
就是它不被接受为文件名。systemd 似乎想要[email protected]
.