How to start a jar file using systemd

6,560

Using systemd directives in place of shell results in a more consistent environment. This includes not relying on $PATH or the working directory by providing full paths where they are known.

Neither & for job control, nor managing SIGHUP is needed. systemd service units are already backgrounded.

Assuming the program doesn't fork, in other words the java exec keeps running, that's Type=simple

[Unit]
Description=something

[Service]
ExecStart=/usr/bin/java -jar /var/www/test.com/something.jar
StandardOutput=file:/var/log/something.out.txt
StandardError=file:/var/log/something.err.txt
Type=simple
WorkingDirectory=/var/www/test.com

[Install]
WantedBy=multi-user.target
Share:
6,560

Related videos on Youtube

Janith
Author by

Janith

Updated on September 18, 2022

Comments

  • Janith
    Janith over 1 year

    I want to start java application on system boot using systemd. I tried to do by adding above scripts. But service is not starting.

    my-startup.service

    [Unit]
    Description=Startup
    
    [Service]
    ExecStart=/usr/local/sbin/my-startup.sh
    
    [Install]
    WantedBy=multi-user.target
    

    my-startup.sh

    cd /var/www/test.com
    nohup java -jar *.jar>test.out 2>test.err &
    

    But application not starting when server starting.

    ● my-startup.service - Startup
       Loaded: loaded (/etc/systemd/system/my-startup.service; enabled; vendor preset: disabled)
       Active: inactive (dead) since Wed 2018-12-12 16:22:52 +0530; 27s ago
      Process: 650 ExecStart=/usr/local/sbin/my-startup.sh (code=exited, status=0/SUCCESS)
     Main PID: 650 (code=exited, status=0/SUCCESS)
    
    Dec 12 16:22:52 localhost.localdomain systemd[1]: Started Startup.
    Dec 12 16:22:52 localhost.localdomain systemd[1]: Starting Startup...
    
    • dortegaoh
      dortegaoh over 5 years
      Have you checked your log files?
    • dortegaoh
      dortegaoh over 5 years
      Have you checked if the jar is actually running? the way you start it systemd can't check it.
    • Janith
      Janith over 5 years
      when I run nohup java -jar *.jar>test.out 2>test.err & alone in the terminal. server is starting
    • Ondřej Xicht Světlík
      Ondřej Xicht Světlík over 5 years
      Try to rewrite your my-startup.sh using full paths /usr/bin/nohup /usr/lib/jvm/java-11/bin/java .... Modify the paths according to your own distro/java installation.
    • Michael Hampton
      Michael Hampton over 5 years
      Why do you have a script at all? You should just start java directly from the systemd unit.
  • Janith
    Janith over 5 years
    This scrips is working fine. But there is a problem in StandardOutput=file:/var/log/something.out.txt StandardError=file:/var/log/something.err.txt. Not created err & out files.
  • Janith
    Janith over 5 years
    I want to put my test.out & test.err inside of project directory. /var/www/test.com. Is it possible?
  • John Mahowald
    John Mahowald over 5 years
    Your program would have to write something to stdout or stderr, and the unit would need to be started by a user with permission to write there (root). Put things where you want, dump it all in /opt or wherever. This is just an example that mostly follows conventions.
  • Janith
    Janith over 5 years
    I put StandardOutput=file:/var/www/test.com/test.out.txt but it doesn't write the log into file. Server starting works fine