代码之家  ›  专栏  ›  技术社区  ›  SageJustus

systemd服务中的ExecStart不支持包含以下内容的文件名*

  •  0
  • SageJustus  · 技术社区  · 1 年前

    sage-server.service

    [Service]
    
    ExecStart=/usr/local/jdk-21.0.2+13/bin/java -jar /home/freedom/local/sage-app*.jar
    

    当我跑步时 systemctl start sage-server ,它失败了。

    错误:无法访问jarfile/home/refreety/local/sage app*.jar

    但如果我跑了 /usr/local/jdk-21.0.2+13/bin/java -jar /home/freedom/local/sage-app*.jar 直接在终端提示下,它是成功的。

    Linux版本:Debian 12

    PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
    NAME="Debian GNU/Linux"
    VERSION_ID="12"
    VERSION="12 (bookworm)"
    VERSION_CODENAME=bookworm
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   tripleee    1 年前

    ExecStart不允许您使用shell功能。。。除非您ExecStart一个shell。

    ExecStart=/bin/sh -c '/usr/local/jdk-21.0.2+13/bin/java -jar /home/freedom/local/sage-app*.jar'
    

    根据您的使用情况,如果可以的话,最好编写特定的文件名,而不是通配符。但如果你不能控制精确的文件名,这是稳健的,因为如果有,它会找到一个;但也很脆弱,因为如果有多个版本,它会找到并尝试通过所有版本,可能与您可能喜欢的顺序正好相反(它们按字母顺序排序,因此在许多版本编号方案中通常是最古老的第一个)。

    如果你只想在最后一场比赛中通过,也许可以这样

    for jar in /home/freedom/local/sage-app*.jar; do
        if ! [ -f "$jar" ]; then
            echo "$0: no match on $jar" >&2
            exit 123
        fi
    done
    exec /usr/local/jdk-21.0.2+13/bin/java "$jar"
    

    用很多话来说,这是在火柴上循环;循环结束后,最后一个将留在循环变量中。

    而您可以将任意复杂的代码放入 sh -c (如果希望所有换行符都在一行中,则需要将换行符替换为分号,除非在 do then )此时最好将代码放在文件中,添加 #!/bin/sh shebang 在顶部, chmod +x 文件,然后将文件名放在 ExecStart=