javanexussystemdnexus3sdkman

How to use system.d service with SDKMan


I am trying to install Sonatype Nexus 3 as a service via system.d and it fails due to INSTALL4J_JAVA_HOME not being set (or at least, so claims systemctl). I use SDKMan! (http://sdkman.io) to manage my Java installation. I am following the Sonatype directions at https://help.sonatype.com/repomanager3/installation/run-as-a-service .

I can run it as the user specified in run_as_user in a bash shell with no difficulties.

How do I make the java home environment variable provided by SDKman visible to system.d?


Solution

  • TL:DR; to resolve, add an Environment section to the system.d service file:

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

    Long-form answer:

    As of this writing, the Sonatype documents say to use the following file as your /etc/systemd/system/nexus.service file, which works great if you installed java to your service user via the system distribution (ensuring your distro's java home is managed by the system). That doesn't work to resolve my question:

    [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
    

    To resolve, you must add the Environment variable to the system.d service file (with your_user replaced by the user you are running nexus as, and /opt/nexus being replaced by your nexus install location):

    [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