linux - 如何为引导时执行启用 systemd 单元模板?

标签 linux templates embedded-linux systemd yocto

我有一个在系统启动时在嵌入式目标上使用 Yocto 兼容文件系统启动的服务器。文件系统使用systemd作为其服务系统。

我的服务器使用 systemd 模板单元文件,但我无法启用它。我们使用模板提供服务器使用的 sqlite 数据库文件的完整路径名。模板单元文件命名为:

/lib/systemd/system/TDI_DataServer@.service

其内容:

[Unit]
    Description=Data Server application (with DB=%I)
    After=syslog.target
[Service]
    Type=simple
    ExecStart=/opt/ndc/DataServer --DEBUG --databaseFile=%I
    Restart=always
    RestartSec=2
[Install]
    WantedBy=multi-user.target

数据库文件在/var/lib/ndc/DataServer.DB中,所以我在/lib/systemd/system中创建了一个符号链接(symbolic link):

# ln -s TDI_DataServer@.service TDI_DataServer@-var-lib-ndc-DataServer.DB.service

当我运行时:

# systemctl start TDI_DataServer@-var-lib-ndc-DataServer.DB

服务器以正确的参数启动,找到数据库,并按预期在后台执行。所以我尝试启用服务模板,以便它会在系统启动时启动服务:

# systemctl enable TDI_DataServer@-var-lib-ndc-DataServer.DB
    Failed to execute operation: No such file or directory

由于失败,我尝试启用模板文件本身:

# systemctl enable TDI_DataServer@
    ln -s '/lib/systemd/system/TDI_DataServer@.service''/etc/systemd/system/multi-user.target.wants/TDI_DataServer@.service'

从表面上看,系统似乎很高兴,但在启动时,systemd 并没有正确执行 TDI_DataServer 的启动。它似乎向单元文件提供了“多用户”参数(可能来自单元文件中的 WantedBy 目标?)。

这是 systemctl 状态:

$ systemctl status -l TDI*
    ● TDI_DataServer@multi-user.service - Data Server application (with DB=multi/user)
       Loaded: loaded (/lib/systemd/system/TDI_DataServer@.service; enabled)
       Active: activating (auto-restart) (Result: signal) since Sat 2000-02-05 20:41:05 UTC; 1s ago
      Process: 2951 ExecStart=/opt/ndc/DataServer --DEBUG --databaseFile=%I (code=killed, signal=ABRT)
     Main PID: 2951 (code=killed, signal=ABRT)

    Feb 05 20:41:05 mityomapl138 DataServer[2951]: [949783265:2047] NOTICE:  per-conn mem: 136 + 2140 headers + protocol rx buf
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: [949783265:2056] NOTICE:  Listening on port 4243
    Feb 05 20:41:05 mityomapl138 systemd[1]: TDI_DataServer@multi-user.service: main process exited, code=killed, status=6/ABRT
    Feb 05 20:41:05 mityomapl138 systemd[1]: Unit TDI_DataServer@multi-user.service entered failed state.
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05: WSServer connected on socket tcp://localhost:5556
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05:  bound to socket inproc://clients
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05: DatabaseWorker using database multi/user
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: unable to open database file
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: terminate called after throwing an instance of 'SQLite::Exception'
    Feb 05 20:41:05 mityomapl138 DataServer[2951]: what():  unable to open database file

● TDI_DataServer.service
       Loaded: loaded (/etc/init.d/TDI_DataServer)
       Active: inactive (dead)

我错过了什么吗?任何帮助将不胜感激。

最佳答案

似乎在 systemctl 解析提供给单元文件的 %I 参数中确实存在错误。如上所述,以下命令失败:

# systemctl enable TDI_DataServer@-var-lib-ndc-DataServer.DB

但是,如果我将/var/lib/ndc 中的数据库文件的名称从“DataServer.DB”更改为“DataServer.db”(即点后的小写,)并且我发出以下命令:

# systemctl enable TDI_DataServer@-var-lib-ndc-DataServer.db

...一切正常。符号链接(symbolic link)在/etc/systemd/system/multi-user.target.wants 中创建,在下次系统重启时,我们的 DataServer 服务会找到它的数据库并愉快地运行。

我已将此事通知我们的第三方文件系统提供商。 Yocto 的人员和 systemd 的维护者可能会对它感兴趣。

关于linux - 如何为引导时执行启用 systemd 单元模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339886/

相关文章:

c++ - 为什么 SFINAE 在它应该工作的地方导致编译器错误?

php - 从 HTML 表单生成 HTML 文件

c++ - 在 BeagleBone 上找不到运行交叉编译程序的 GLIBCXX_3.4.26

c++ - 从 netlink 套接字请求连接设备列表

linux - 如何知道 Linux 中特定引脚的中断/GPIO 编号

Linux 30 位随机数生成器

linux - AndroidStudio - 错误 - llij.ide.plugins.PluginManager - java.lang.NullPointerException

linux - 创建 Dockerfile 并在 Linux 镜像上挂载 azure 文件共享

c++ - 使用指针的模板函数

linux - Fedora 上调用 GNOME 失败对话框的可执行文件在哪里?