Run java jar file on server as background process

I need to run java jar on the server in order to communicate between two applications. I wrote two shell scripts to run it, but as soon as I run this script, I cannot finish / end the process. If I press ctrl + C or close the console, the server will shut down. Can someone help me modify this script to run as a normal server?

#!/bin/sh java -jar /web/server.jar echo $! #> startupApp.pid 
+69
unix shell executable-jar
Aug 24 2018-12-12T00:
source share
3 answers

You can try the following:

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

Symbol and, switches the program to run in the background.

The nohup utility makes the command passed as an argument, executed in the background even after logging out.

+158
Aug 24 2018-12-12T00:
source share

If you use Ubuntu and have "Upstart" (http://upstart.ubuntu.com/).you can try the following:

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 . You can /var/log/yourservice.log to make sure it works.

If you just want to start your jar from the console without starting the console window, you can simply do:

 java -jar /web/server.jar > /var/log/yourservice.log 2>&1 
+25
Aug 24 2018-12-12T00:
source share

Systemd , which now runs on most distributions

Step 1:

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

Step 2:

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

Step 3:

Put the following Template into 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:

Start the service
as superuser

 $ 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 
+18
Feb 24 '15 at 19:16
source share



All Articles