编辑1

编辑1

假设在启动时启用以下一次性服务,

[Unit]

Description = My Server Daemon
Documentation = https://etcetc.org

Wants = network-online.target getty.target NetworkManager-wait-online.service isc-dhcp-server.service xinetd.service smbd.service
After = network-online.target getty.target NetworkManager-wait-online.service isc-dhcp-server.service xinetd.service smbd.service

[Service]

Type = oneshot

RemainAfterExit = yes

User = foouser

PIDFile = /run/myd.pid

WorkingDirectory = /home/foouser/my-server/bin

ExecStart = /home/foouser/my-server/bin/myd
ExecStop = /home/foouser/my-server/bin/abort.sh

TimeoutStopSec = 5

[Install]

WantedBy = multi-user.target

它实际上无法启动。过滤/var/log/syslog

systemd[1]: myd.service: 步骤 CHDIR 生成 /home/foouser/my-server/bin/my 失败: 没有此文件或目录

虽然journalctl --unit=myd.service同意

systemd[1]: myd.service: 主进程已退出, 代码=已退出, 状态=200/CHDIR

因此,由于/home/foouser/my-server/bin/myd是我的软链接/home/foouser/my-server/xspyctra/xspyctrad.py,因此我

  1. 已更改ExecStart = /home/foouser/my-server/xspyctra/xspyctrad.py
  2. 评论工作目录我其实对此不感兴趣,因为 Python 脚本会os.chdir('/home/foouser/my-server/bin')自己执行

服务重新加载,系统重启等。但现在我被以下错误惊呆了

systemd[1653]: myd.service: 步骤 EXEC 生成 /home/foouser/my-server/xspyctra/xspyctrad.py 失败: 没有此文件或目录

systemd[1]: myd.service: 主进程已退出, 代码=已退出, 状态=203/EXEC

我可以肯定该文件存在

$ ls -l /home/foouser/my-server/xspyctra/xspyctrad.py
-rwxrw-r-- 1 foouser foouser 5532 May 10 16:13 /home/foouser/my-server/xspyctra/xspyctrad.py

作为最后的诊断,service myd status

● myd.service - My Server Daemon
  Loaded: loaded (/etc/systemd/system/myd.service; enabled; vendor preset: enabled)
  Active: failed (Result: exit-code) since Fri 2018-05-11 10:58:21 CEST; 16min ago
  Docs: https://etcetc.org
  Main PID: 1627 (code=exited, status=203/EXEC)

编辑1

然而,这可能只发生在启动时。事实上,

sudo service myd stop
sudo service myd start

似乎有效,因为service myd status

● myd.service - My Server Daemon
     Loaded: loaded (/etc/systemd/system/myd.service; enabled; vendor preset: enabled)
     Active: activating (start) since Fri 2018-05-11 11:22:52 CEST; 1min 16s ago
       Docs: https://etcetc.org
   Main PID: 2106 (python2)
      Tasks: 24
     Memory: 545.1M
        CPU: 10.913s
     CGroup: /system.slice/myd.service
       ├─2106 python2 /home/foouser/my-server/bin/myd
       ├─2122 ./<my-child-process-a>
       ├─2127 ./<my-child-process-b>
       └─2133 ./<my-child-process-c>

正如预期的那样。此外,请注意xspyctrad.py,实际脚本将生成twisted.reactor,因此不会返回并停留在前台。

那么我的错误可能出在哪里?

答案1

除了将可执行文件移动到早期可访问的位置(无论如何这都是一件好事)之外,还可以指定挂载依赖项来告诉 systemd 延迟启动您的设备,直到该位置挂载并可用之后。

您可以使用RequiresMountsFor=

[Unit]
RequiresMountsFor=/home/foouser/my-server/bin

或者指定Requires=/After=相应的.mount单元,如果你确切知道什么需要安装:

[Unit]
Requires=home-foouser.mount
After=home-foouser.mount

答案2

正如 Kamil 指出的那样,/home/foouser/在服务启动时不存在。移至现有位置可解决该问题。

相关内容