Systemd Socket 激活 触发 bash 脚本

Systemd Socket 激活 触发 bash 脚本

我想定期触发远程 bash 脚本。其工作原理是,第 3 方应用程序将连接到特定 TCP 端口上的 CentOS 7 系统并发送一条短文本消息。由于第三方应用程序的原因,SSH 不是一个选项。

收到消息后,需要将 IP 地址传递给 bash 脚本。我希望 bash 脚本运行,然后进入休眠状态,直到下一条消息。我不想写守护进程。我只是想让这个简单。

这些消息可能每周出现几次或更少。我们使用 xinetd 运行它,但不确定如何使其与 systemd 一起工作。

这是我到目前为止所拥有的:

/etc/systemd/systemfoo.service

[Unit]
Description=Foo Service
After=network.target foo.socket
Requires=foo.socket

[Service]
Type=oneshot
ExecStart=/bin/bash /opt/foo/foo.sh
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target

/etc/systemd/systemfoo.socket

[Unit]
Description=Foo Socket
PartOf=foo.service

[Socket]
ListenStream=127.0.0.1:7780

[Install]
WantedBy=sockets.target

/opt/foo/foo.sh

#!/bin/bash

# Not sure how to get IP
logger -t FOO "Connection received:"
# Do some action

这是我在日志中看到的内容:

Jul 10 17:29:32 localhost systemd: Listening on Foo Socket.
Jul 10 17:29:32 localhost systemd: Starting Foo Socket.
Jul 10 17:29:32 localhost systemd: Started Foo Service.
Jul 10 17:29:32 localhost systemd: Starting Foo Service...
Jul 10 17:29:32 localhost FOO: Connection received
Jul 10 17:29:53 localhost systemd: Started Session 4 of user vagrant.
Jul 10 17:29:53 localhost systemd-logind: New session 4 of user vagrant.
Jul 10 17:29:53 localhost systemd: Starting Session 4 of user vagrant.
Jul 10 17:29:56 localhost su: (to root) vagrant on pts/1
Jul 10 17:30:11 localhost systemd: Started Foo Service.
Jul 10 17:30:11 localhost systemd: Starting Foo Service...
Jul 10 17:30:11 localhost FOO: Connection received
Jul 10 17:30:11 localhost systemd: Started Foo Service.
Jul 10 17:30:11 localhost systemd: Starting Foo Service...
Jul 10 17:30:11 localhost FOO: Connection received
Jul 10 17:30:11 localhost systemd: Started Foo Service.
Jul 10 17:30:11 localhost systemd: Starting Foo Service...
Jul 10 17:30:11 localhost FOO: Connection received
Jul 10 17:30:11 localhost systemd: Started Foo Service.
Jul 10 17:30:11 localhost systemd: Starting Foo Service...
Jul 10 17:30:11 localhost FOO: Connection received
Jul 10 17:30:11 localhost systemd: Started Foo Service.
Jul 10 17:30:11 localhost systemd: Starting Foo Service...
Jul 10 17:30:11 localhost FOO: Connection received
Jul 10 17:30:11 localhost systemd: start request repeated too quickly for foo.service
Jul 10 17:30:11 localhost systemd: Failed to start Foo Service.
Jul 10 17:30:11 localhost systemd: Unit foo.socket entered failed state.
Jul 10 17:30:11 localhost systemd: Unit foo.service entered failed state.
Jul 10 17:30:11 localhost systemd: foo.service failed.

关于如何让 systemd 运行脚本一次然后等待下一条消息然后再次运行它有什么建议吗?

为了测试我只是运行:

echo "Hello" | nc 127.0.0.1 7780

更新的工作配置

/etc/systemd/[电子邮件受保护] 注意@。

[Unit]
Description=Foo Service
After=network.target systemfoo.socket
Requires=systemfoo.socket

[Service]
Type=oneshot
ExecStart=/bin/bash /opt/foo/foo.sh
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target

/etc/systemd/systemfoo.socket

[Unit]
Description=Foo Socket
[email protected]

[Socket]
ListenStream=127.0.0.1:7780
Accept=Yes

[Install]
WantedBy=sockets.target

/opt/foo/foo.sh

#!/bin/bash

# Not sure how to get IP
logger -t FOO "Connection received: $REMOTE_ADDR $REMOTE_PORT"
# Do some action

需要将配置加载到 systemd 中:

systemctl enable systemfoo.socket
systemctl start systemfoo.socket

答案1

你应该添加Accept=yessocket单元,指示systemd应该为每个连接启动单独的服务实例,然后将服务单元变成模板([email protected]),为每个连接单独实例化。然后,根据联机帮助页,远程地址和端口应该在REMOTE_ADDR和环境变量中可用。REMOTE_PORTsystemd.socket(5)

相关内容