Run java jar file on a server as background process

ShellUnixExecutable Jar

Shell Problem Overview


I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can't shut down / terminate the process. If I press ctrl+C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?

 #!/bin/sh
java -jar /web/server.jar
echo $! 
#> startupApp.pid

Shell Solutions


Solution 1 - Shell

You can try this:

#!/bin/sh
nohup java -jar /web/server.jar &

The & symbol, switches the program to run in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.

Solution 2 - Shell

Systemd which now runs in the majority of distros

Step 1:

Find your user defined services mine was at /usr/lib/systemd/system/

Step 2:

Create a text file with your favorite text editor name it whatever_you_want.service

Step 3:

Put following Template to the file whatever_you_want.service

[Unit]
Description=webserver Daemon

[Service]
ExecStart=/usr/bin/java -jar /web/server.jar
User=user

[Install]
WantedBy=multi-user.target

Step 4:

Run your service
as super user

$ systemctl start whatever_you_want.service # starts the service
$ systemctl enable whatever_you_want.service # auto starts the service
$ systemctl disable whatever_you_want.service # stops autostart
$ systemctl stop whatever_you_want.service # stops the service
$ systemctl restart whatever_you_want.service # restarts the service

Solution 3 - Shell

If you're using Ubuntu and have "Upstart" (http://upstart.ubuntu.com/) you can try this:

Create /var/init/yourservice.conf

with the following content

description "Your Java Service"  
author "You"  
  
start on runlevel [3]  
stop on shutdown  
  
expect fork  
  
script     
    cd /web 
    java -jar server.jar >/var/log/yourservice.log 2>&1  
    emit yourservice_running  
end script  

Now you can issue the service yourservice start and service yourservice stop commands. You can tail /var/log/yourservice.log to verify that it's working.

If you just want to run your jar from the console without it hogging the console window, you can just do:

java -jar /web/server.jar > /var/log/yourservice.log 2>&1

Solution 4 - Shell

Run in background and add logs to log file using the following:

nohup java -jar /web/server.jar > log.log 2>&1 &

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBernad AliView Question on Stackoverflow
Solution 1 - ShellAnton BeloglazovView Answer on Stackoverflow
Solution 2 - ShellmteleshaView Answer on Stackoverflow
Solution 3 - ShellStrelokView Answer on Stackoverflow
Solution 4 - ShellKayVView Answer on Stackoverflow