- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建我的第一个后台服务,并且想通过套接字与之通信。
我有以下脚本/tmp/myservice.sh
:
#! /usr/bin/env bash
while read received_cmd
do
echo "Received command ${received_cmd}"
done
和以下套接字
/etc/systemd/user/myservice.socket
[Unit]
Description=Socket to communicate with myservice
[Socket]
ListenSequentialPacket=/tmp/myservice.socket
以及以下服务:
[Unit]
Description=A simple service example
[Service]
ExecStart=/bin/bash /tmp/myservice.sh
StandardError=journal
StandardInput=socket
StandardOutput=socket
Type=simple
这个想法是要了解如何与后台服务进行通信,这里使用unix文件套接字。该脚本从 shell 启动并读取stdin时效果很好,我认为通过设置
StandardInput = "socket"
可以从套接字以相同方式读取。
nc -U /tmp/myservice.socket
时,该命令立即返回,并且我得到以下输出:
$ journalctl --user -u myservice
-- Logs begin at Sat 2020-10-24 17:26:25 BST, end at Thu 2020-10-29 14:00:53 GMT. --
Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
Oct 29 08:40:16 shiny bash[21941]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
Oct 29 08:40:16 shiny bash[21942]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
Oct 29 08:40:16 shiny bash[21943]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
Oct 29 08:40:16 shiny bash[21944]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
Oct 29 08:40:16 shiny bash[21945]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Start request repeated too quickly.
Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Failed with result 'start-limit-hit'.
Oct 29 08:40:16 shiny systemd[1689]: Failed to start A simple service example.
我是否误解了套接字的工作方式?为什么
read
无法从套接字读取?我是否应该使用另一种机制与后台服务进行通信(如我所说,这是我的第一个后台服务,所以我可以在这里做一些非常规的事情)?
最佳答案
我看到的使用shell脚本的唯一一件事就是ListenStream=
而不是ListenSequentialPacket=
。 (显然,这意味着您会丢失数据包边界,但是读取 shell 程序通常会定向为从流中读取以\n
结尾的行,因此通常不会出现问题)。
但是最重要的是缺少的是额外的Accept
行:
[Socket]
ListenStream=...
Accept=true
据我了解,没有此服务,服务将被传递给套接字,它必须首先在其上进行套接字
accept()
调用,以获得实际的连接套接字(因此
read
错误)。然后,该服务还必须处理所有其他连接。
Accept=true
,将为每个新连接启动一个新服务,并将该服务传递给立即可用的套接字。但是请注意,这意味着该服务现在必须被模板化,即称为
myservice@.service
而不是
myservice.service
。
Accept
保留默认值为false)。参见
man systemd.socket
。
关于bash - 通过映射到stdin的套接字与Systemd服务进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64594843/
我想在 systemd 的 myapp.service 文件中使用多个命令 [Unit] Description=to serve myapp [Service] User=ubuntu Workin
我想为 systemd 服务启用核心转储生成,该服务提供 SEGV 状态并退出。 scd.service: main process exited, code=killed, status=11/SE
我有一个由 systemd 运行的程序,其服务文件如下所示: [Unit] Description=... [Service] Type=notify ExecStart=/usr/sbin/mypr
我有一个 systemd 单元,参数中有空格 ExecStart=command --argument="text text" systemd 似乎无法识别双引号或单引号,并将参数拆分为两个参数。知道
我正在使用一个 systemd 服务,它在“启动”时调用一个进程(例如 systemctl start test.service)。根据设计,进程永远处于循环状态,我们可以使用 ps 命令查看进程的存
我的解决方案(到目前为止)是从公共(public) session 中评论 pam_systemd.so 。一切都在 system.slice 中运行,没有对照组。我还不确定这会产生什么影响,但至少一
我四处查看并用 google 搜索,找不到节点导出器公开的 systemd 指标列表? 最佳答案 node_systemd 指标列表: node_systemd_unit_state - System
我正在尝试将文件写入磁盘作为 ExecStartPre 的一部分systemd 单元文件的命令。 这就是我正在使用的: [Unit] Description=My service [Service]
我的 journalctl到处都是gnome-session警告。我已将问题追溯到谷歌浏览器,警告相对无害。然而,它淹没了我的日志输出,坦率地说,如果我确实需要检查它,我将无法找到我需要的东西。 Ma
操作系统级别:CentOS Linux release 7.4.1708普罗米修斯等级:2.4.2prometheus.service: [Unit] Description=Prometheus [
我们有一个 systemd 服务,根据建议的配置 here 来管理 Puma 进程。 . 虽然看起来运行良好,但重新启动超时,因此看起来失败,即使它们是成功的。如何调试这一类问题? systemd 期
systemd 可以让 rsyslog 守护进程保持事件状态,即使它死了,这真是太棒了。 但是我正在制作一个新的 rsyslog 配置文件,并且需要在 Debug模式下启动此 rsyslog 守护进程
我想更改 systemd-logind 服务的环境参数。这不在 configuration.nix 中可用的登录选项中,所以我尝试了 systemd = { services.system
我试图了解我的新项目中不同 systemd 服务之间的依赖关系。我们正在使用 yocto 构建系统和 systemd/系统服务。 如果我表现出色, systemctl -l 它将按字母顺序列出所有服务
我正在尝试在 lindux debian 上安装和配置 systemD,但在安装 systemd-sysv 时出现错误,我知道这是让 systemD 管理服务所必需的。 当我运行时 apt-get i
我有两个服务 A 和 B。 A 在启动时在 etcd 中设置一个值,比如它从环境文件中获取的公共(public) IP 地址: ExecStartPost=/usr/bin/etcdctl set /
我们目前在一台主机上运行着许多不同的服务,我们正在广泛使用 systemd,包括用于消息传递的 systemd-notify 和用于服务管理的自己的前端。 我们希望开始在容器内运行这些服务,以简化依赖
我们目前在一台主机上运行着许多不同的服务,我们正在广泛使用 systemd,包括用于消息传递的 systemd-notify 和用于服务管理的自己的前端。 我们希望开始在容器内运行这些服务,以简化依赖
几个月前,我使用 systemd.journal 编写了一个小型 Python 服务。 Pip list 显示我在相应的 virtualenv 中安装了 python-systemd (231)。 当
我想启动一个不存在的计时器,它应该在当时启动一个不存在的服务。这应该通过 systemd 临时单元(如 systemd-run)来完成。 当我执行以下代码时,我收到一个异常(见下文)。有人可以告诉我,
我是一名优秀的程序员,十分优秀!