条件失败时重试 systemd

条件失败时重试 systemd

我有一个 systemd 服务,它检查启动时是否安装了外部媒体。

[Unit]
Description=My App
ConditionPathIsMountPoint=/mnt/sdcard

[Service]
WorkingDirectory=/opt/myapp
ExecStartPre=+/opt/myapp/bin/pre.sh
ExecStart=/opt/myapp/bin/myapp
ExecStop=/bin/kill -INT ${MAINPID}
ExecReload=/bin/kill -INT ${MAINPID} && /opt/myapp/bin/myapp
Restart=always
RestartSec=5
User=user
Group=user

[Install]
WantedBy=multi-user.target mnt-sdcard.mount

我想等待myapp启动直到/mnt/sdcard安装。但这项服务效果并不好。当安装介质被延迟时,systemd 会抛出异常start condition failed并且不会重试。

# systemctl status myapp
● myapp.service - My App
   Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
Condition: start condition failed at Wed 2021-01-27 14:22:31 JST; 2h 35min ago
            ConditionPathIsMountPoint=/mnt/sdcard was not met

当启动条件失败时如何重试启动?

答案1

在您的预启动脚本中检查外部媒体是否延迟安装在循环中

答案2

如果需要在启动时安装 SD 卡,您可以添加以下内容来代替条件。

Requires=mnt-sdcard.mount

mnt-sdcard.mount从......中去除WantedBy。不知道它有什么作用。

相关内容