【问题标题】:How to use system.d service with SDKMan如何通过 SDKMan 使用 system.d 服务
【发布时间】:2021-05-11 21:17:30
【问题描述】:

我正在尝试通过 system.d 将 Sonatype Nexus 3 作为服务安装,但由于未设置 INSTALL4J_JAVA_HOME(或至少如此声明 systemctl)而失败。我使用 SDKMan! (http://sdkman.io) 来管理我的 Java 安装。我在https://help.sonatype.com/repomanager3/installation/run-as-a-service 遵循 Sonatype 的指示。

我可以在 bash shell 中以run_as_user 中指定的用户身份毫无困难地运行它。

如何使 SDKman 提供的 java home 环境变量对 system.d 可见?

【问题讨论】:

    标签: java nexus systemd nexus3 sdkman


    【解决方案1】:

    TL:DR;要解决此问题,请在 system.d 服务文件中添加 Environment 部分:

    [Service]
    Environment="INSTALL4J_JAVA_HOME=/home/nexus/.sdkman/candidates/java/current/"
    

    长答案:

    在撰写本文时,Sonatype 文档说使用以下文件作为您的 /etc/systemd/system/nexus.service 文件,如果您通过系统分发将 java 安装到您的服务用户(确保您的发行版的 java home 由系统管理),这将非常有用)。这不能解决我的问题:

    [Unit]
    Description=nexus service
    After=network.target
    
    [Service]
    Type=forking
    LimitNOFILE=65536
    ExecStart=/opt/nexus/bin/nexus start
    ExecStop=/opt/nexus/bin/nexus stop
    User=nexus
    Restart=on-abort
    TimeoutSec=600
    
    [Install]
    WantedBy=multi-user.target
    

    要解决此问题,您必须将环境变量添加到 system.d 服务文件(your_user 替换为您正在运行的用户 nexus/opt/nexus 替换为您的 nexus 安装位置):

    [Unit]
    Description=nexus service
    After=network.target
    
    [Service]
    Environment="INSTALL4J_JAVA_HOME=/home/your_user/.sdkman/candidates/java/current/"
    Type=forking
    LimitNOFILE=65536
    ExecStart=/opt/nexus/bin/nexus start
    ExecStop=/opt/nexus/bin/nexus stop
    User=your_user
    Restart=on-abort
    TimeoutSec=600
    
    [Install]
    WantedBy=multi-user.target
    

    【讨论】: