Systemd

簡單的 systemd 服務和套接字失敗

  • March 11, 2021

我有一個服務,我試圖以這種方式執行,但它是一個稍微大一點的 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.socketsystemctl start testPy.socket步驟?我發現的一件事我還不太明白,那testPy.service就是它不被接受為文件名。systemd 似乎想要testPy@.service.

引用自:https://serverfault.com/questions/1056753