我有一个简单的套接字/服务对,其中服务依赖于来自套接字的标准输入:
# /etc/systemd/system/simple.socket
[Unit]
Description=Simple socket
[Socket]
ListenStream=11111
# /etc/systemd/system/simple.service
[Unit]
Description=Simple service
[Service]
Type=simple
ExecStart=-cat -
StandardInput=socket
StandardOutput=socket
当我尝试启动时,netcat 127.0.0.1 11111
我得到:
- simple.service - "Simple service"
Loaded: loaded (/etc/systemd/system/simple.service; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Tue 2020-05-19 09:42:17 CEST; 11min ago
TriggeredBy: simple.socket
Process: 400879 ExecStart=/bin/cat - (code=exited, status=1/FAILURE)
Main PID: 400879 (code=exited, status=1/FAILURE)
- simple.socket - "Simple socket"
Loaded: loaded (/etc/systemd/system/simple.socket; static; vendor preset: enabled)
Active: failed (Result: service-start-limit-hit) since Tue 2020-05-19 09:42:17 CEST; 11min ago
Triggers: simple.service
Listen: [::]:11111 (Stream)
我的猜测是cat
无法启动,因为它正在尝试连接到stdin
不可用的连接。如果我替换cat -
为sleep infinity
服务,则启动正常。我不明白为什么
答案1
Accept=
该解决方案与套接字选项有关。
这个版本可以工作:
# /etc/systemd/system/simple.socket
[Unit]
Description=Simple socket
[Socket]
Accept=yes
ListenStream=11111
记下新的文件路径
# /etc/systemd/system/[email protected]
[Unit]
Description=Simple service
[Service]
Type=simple
ExecStart=-cat -
StandardInput=socket
StandardOutput=socket
现在nc 127.0.0.1 11111
不返回,让您回显您发送的任何内容。现在还systemctl status simple*
给出:
- simple.socket - Simple socket
Loaded: loaded /etc/systemd/system/simple.socket; static: vendor preset: enabled)
Active: active (listening) since Tue 2020-05-19 10:04:48 CEST; 48s ago
Triggers: [email protected]:11111-127.0.0.1:47194.service
Listen: [::]:11111 (Stream)
Accepted: 2; Connected: 1;
Tasks: 0 (limit: 9429)
Memory: 120.0K
CGroup: /system.slice/simple.socket
- [email protected]:11111-127.0.0.1:47194.service - "Simple service" (127.0.0.1:47194)
Loaded: loaded /etc/systemd/system/[email protected]; static: vendor preset: enabled)
Active: active (running) since Tue 2020-05-19 10:04:48 CEST; 48s ago
TriggeredBy: simple.socket
Main PID: 403830 (cat)
Tasks: 1 (limit: 9429)
Memory: 184.0K
CGroup: /system.slice/system-simple.slice/[email protected]:11111-127.0.0.1:47194.service
-403830 /bin/cat -
我认为这是可行的,因为当 时Accept=no
,每个连接都尝试连接到同一进程,并且尝试将多个套接字复用到单个标准输入中有点奇怪。每个Accept=yes
连接都会产生一个新进程,因此可以很简单地将套接字通过管道传输到每个进程的标准输入中。